κ°μ²΄μ§ν₯
1.
ν΄λμ€
2.
κ°μ²΄
3.
μμ±μ
4.
μμ
5.
μΆμ ν΄λμ€
6.
λ―Ήμ€μΈ
7.
enum
8.
static
ν΄λμ€
κ°μ²΄ μ μνλ μ€κ³λ
// Person ν΄λμ€ μ μ
class Person {
// λ©€λ² λ³μ (μμ±)
String name;
int age;
// μμ±μ
Person(this.name, this.age);
// λ©€λ² λ©μλ
void introduceYourself() {
print('Hello, my name is $name and I am $age years old.');
}
}
void main() {
// Person ν΄λμ€μ μΈμ€ν΄μ€ μμ±
var person1 = Person('Alice', 25);
// μΈμ€ν΄μ€μ μμ±μ μ κ·Ό
print('Name: ${person1.name}, Age: ${person1.age}');
// μΈμ€ν΄μ€μ λ©μλ νΈμΆ
person1.introduceYourself(); // μΆλ ₯: Hello, my name is Alice and I am 25 years old.
// λ λ€λ₯Έ μΈμ€ν΄μ€ μμ±
var person2 = Person('Bob', 30);
person2.introduceYourself(); // μΆλ ₯: Hello, my name is Bob and I am 30 years old.
}
Dart
볡μ¬
κ°μ²΄
μ€μΈκ³μ μμ±κ³Ό κΈ°λ₯μ, νλ‘κ·Έλ¨μ λ³μμ λ©μλλ‘ μΆμνν κ²
κ°μ²΄ μμ±
var κ°μ²΄λͺ
= κ°μ²΄λͺ
(μΈμ1, μΈμ2);
Dart
볡μ¬
β’
μμμ½λ
void main() {
// Person ν΄λμ€μ μΈμ€ν΄μ€ μμ±
var person1 = Person('Alice', 25);
// λ λ€λ₯Έ μΈμ€ν΄μ€ μμ±
var person2 = Person('Bob', 30);
// κ°μ²΄μ μμ±μ μ κ·Ό
print('Person 1: Name - ${person1.name}, Age - ${person1.age}');
print('Person 2: Name - ${person2.name}, Age - ${person2.age}');
// κ°μ²΄μ λ©μλ νΈμΆ
person1.introduceYourself(); // μΆλ ₯: Hello, my name is Alice and I am 25 years old.
person2.introduceYourself(); // μΆλ ₯: Hello, my name is Bob and I am 30 years old.
}
Dart
볡μ¬
μμ±μ
κ°μ²΄λ₯Ό μμ±ν λ, λ³μ λλ λ©μλλ₯Ό μ΄κΈ°ννκΈ° μν΄μ μ€νλλ λ©μλ
κ°μ²΄λ₯Ό μ΄κΈ°ννλ νΉμν λ©μλ
ν΄λμ€μ μ΄λ¦κ³Ό λμΌν μ΄λ¦μ κ°μ§λ©°, 맀κ°λ³μλ₯Ό κ°μ§ μ μμ΅λλ€.
ν΄λμ€μ λͺ
μμ μΈ μμ±μκ° μλ κ²½μ° κΈ°λ³Έ μμ±μκ° μλμΌλ‘ μμ±λ©λλ€.
class Person {
String name;
int age;
Person(this.name, this.age); // μμ±μ
void sayHello() {
print('Hello, $name!');
}
}
void main() {
var person = Person('John', 25);
person.sayHello(); // μΆλ ₯: Hello, John!
}
Dart
볡μ¬
μΆμ ν΄λμ€
ν΄λμ€μ κ³μΈ΅μ μΈ μ€κ³λ₯Ό μν΄, ꡬν μ½λκ° μλ μΆμ λ©μλλ‘ κ΅¬μ±λ ν΄λμ€
μΆμ ν΄λμ€ ν€μλ
abstract
Dart
볡μ¬
abstract class Shape {
double getArea(); // μΆμ λ©μλ
}
class Circle extends Shape {
double radius;
Circle(this.radius);
double getArea() {
return 3.14 * radius * radius;
}
}
Dart
볡μ¬
μΆμ ν΄λμ€ ν€μλλ ν΄λμ€λΏλ§μλλΌ λ―Ήμ€μΈμλ μ μ©κ°λ₯νλ€.
abstract mixin class Musician {
// No 'on' clause, but an abstract method that other types must define if
// they want to use (mix in or extend) Musician:
void playInstrument(String instrumentName);
void playPiano() {
playInstrument('Piano');
}
void playFlute() {
playInstrument('Flute');
}
}
class Virtuoso with Musician { // Use Musician as a mixin
void playInstrument(String instrumentName) {
print('Plays the $instrumentName beautifully');
}
}
class Novice extends Musician { // Use Musician as a class
void playInstrument(String instrumentName) {
print('Plays the $instrumentName poorly');
}
}
Dart
볡μ¬
μμ
λΆλͺ¨ ν΄λμ€μ λ³μμ λ©μλλ₯Ό μμ ν΄λμ€μμ μ¬μ¬μ©νλ κ²
class Animal {
void makeSound() {
print('Some generic sound');
}
}
class Dog extends Animal {
void makeSound() {
print('Woof! Woof!');
}
}
void main() {
var dog = Dog();
dog.makeSound(); // μΆλ ₯: Woof! Woof!
}
Dart
볡μ¬
λ―Ήμ€μΈ(Minin)
λ€μ€ μμμ μ§μνλ κΈ°λ²
mixin Flying {
void fly() {
print('Flying high!');
}
}
class Bird with Flying {
// Bird ν΄λμ€μ Flying λ―Ήμ€μΈ μΆκ°
}
void main() {
var bird = Bird();
bird.fly(); // μΆλ ₯: Flying high!
}
Dart
볡μ¬
// Flying λ―Ήμ€μΈ
mixin Flying {
void fly() {
print('Flying high!');
}
}
// Swimming λ―Ήμ€μΈ
mixin Swimming {
void swim() {
print('Swimming gracefully!');
}
}
// Bird ν΄λμ€μ Flying λ―Ήμ€μΈ μ μ©
class Bird with Flying {
String name;
Bird(this.name);
}
// Fish ν΄λμ€μ Swimming λ―Ήμ€μΈ μ μ©
class Fish with Swimming {
String name;
Fish(this.name);
}
// Duck ν΄λμ€μ Flyingκ³Ό Swimming λ―Ήμ€μΈ μ μ© (λ€μ€ μμ)
class Duck with Flying, Swimming {
String name;
Duck(this.name);
}
void main() {
var eagle = Bird('Eagle');
eagle.fly(); // μΆλ ₯: Flying high!
var salmon = Fish('Salmon');
salmon.swim(); // μΆλ ₯: Swimming gracefully!
var mallard = Duck('Mallard');
mallard.fly(); // μΆλ ₯: Flying high!
mallard.swim(); // μΆλ ₯: Swimming gracefully!
}
Dart
볡μ¬
λ―Ήμ€μΈμ λνμ¬λ λ©μλ μ€λ²λΌμ΄λ©μ΄ κ°λ₯νλ€.
// Flying λ―Ήμ€μΈ
mixin Flying {
void fly() {
print('Default flying behavior');
}
}
// Bird ν΄λμ€μ Flying λ―Ήμ€μΈ μ μ©
class Bird with Flying {
String name;
Bird(this.name);
// Flying λ―Ήμ€μΈμ fly λ©μλλ₯Ό μ€λ²λΌμ΄λ©
void fly() {
print('$name is flying high!');
}
}
void main() {
var eagle = Bird('Eagle');
eagle.fly(); // μΆλ ₯: Eagle is flying high!
}
Dart
볡μ¬
enum
μμλ€μ μ΄κ±°νμ¬ λ¬Άμ΄λμ νμ
enum Weekday { monday, tuesday, wednesday, thursday, friday }
void main() {
var today = Weekday.wednesday;
print(today); // μΆλ ₯: Weekday.wednesday
}
Dart
볡μ¬
static
λ³μ λλ λ©μλλ₯Ό νλ‘κ·Έλ¨ μ€ν μ, κ°μ₯ λ¨Όμ λ©λͺ¨λ¦¬μ κ³ μ νλ ν€μλ
class Calculator {
static double add(double a, double b) {
return a + b;
}
}
void main() {
var result = Calculator.add(5.0, 3.0);
print(result); // μΆλ ₯: 8.0
}
Dart
볡μ¬