class 생성
class Fruit{}
특성 (property) 추가
class Fruit{
String? color;
String? taste;
String? name;
}
행위 (메소드) 추가
class Fruit{
String? color;
String? taste;
String? name;
void eat() {
print("The taste of $name is $taste");
}
}
객체 생성
class Fruit{
String? color;
String? taste;
String? name;
void eat() {
print("The taste of $name is $taste");
}
}
void main() {
final apple = Fruit();
}
객체에 속성 주입
class Fruit{
String? color;
String? taste;
String? name;
void eat() {
print("The taste of $name is $taste");
}
}
void main() {
final apple = Fruit();
apple.color = "Red";
apple.taste = "Sweet";
apple.name = "Apple";
}
객체에 행동 주입
class Fruit{
String? color;
String? taste;
String? name;
void eat() {
print("The taste of $name is $taste");
}
}
void main() {
final apple = Fruit();
apple.color = "Red";
apple.taste = "Sweet";
apple.name = "Apple";
apple.eat();
// cascode operator 사용 가능
final banana = Fruit()
..color = "Yello"
..taste = "Sweet"
..name = "Banana"
..eat();
}
: 매개변수가 없는 constructor
class Fruit {
Fruit(){
print("Fruit is Created");
}
String? color;
String? taste;
String? name;
void eat() {
print("The taste of $name is $taste");
}
}
class Fruit {
Fruit({String color, String taste, String name}){
this.color = color;
this.taste = taste;
this.name = name;
}
}
void main() {
final apple = Fruit(color: "Red", name: "Apple", taste: "Sweet");
}
다른 기능을 수행하기 위해서 둘 이상의 constrcutor가 필요할 때 사용
class Fruit {
Fruit.foo(){}
Fruit.bar(){}
//...
}