class Item{
String name='';
int damage=0;
int price=0;
Item();
Item.setData(String name, int price, {int damage=0}){
this.name = name;
this.damage = damage;
this.price = price;
}
void showInfo(){
print("name: $name, damage: $damage, price: $price");
}
}
class Sword extends Item{
@override
int damage=10;
Sword();
Sword.setData(String name, int price, int damage) : super.setData(name, price, damage:damage);
}
class Shield extends Item{
int damage = 0;
Shield();
Shield.setData(String name, int price, int damage) : super.setData(name, price, damage:damage);
}
class Bag{
List<Item> item = [];
Bag();
void addItem(Item item){
this.item.add(item);
}
void removeItem(Item item){
this.item.remove(item);
}
void showInfo(){
print(item);
item.forEach((e){
//print(e.name);
e.showInfo();
});
}
}
void main(){
Sword knif = new Sword.setData("knif", 100, 10);
Shield shield = new Shield.setData("wooden shield", 150, 0);
//shield.showInfo();
//knif.showInfo();
Bag bag = new Bag();
bag.addItem(knif);
bag.addItem(shield);
bag.removeItem(knif);
bag.showInfo();
}
'IT News > Flutter & Dart' 카테고리의 다른 글
Dart 언어에서 Operator(오퍼레이터) 의미 (1) | 2021.12.24 |
---|---|
Dart Class 기본 예제 (0) | 2021.09.06 |
함수(function)와 메쏘드(method)의 차이점 (0) | 2021.08.27 |