
예시1)
abstract class Animal {
void eat();
void sleep();
// 함수를 구현하지 않은 형태
void run(){
print('Animal.run 도망간다');
}
}
// 함수를 구현한 형태
// Animal 추상 클래스 생성
class Dog extends Animal{
void eat() {}
void sleep() {}
void run() {
super.run();
}
}
// Animal을 상속받는 Dog 클래스 생성
void main () {
Dog myDog = Dog() ;
myDog.run();
}
// 함수 실행
예시2)
추상클래스를 사용하여 IronMan Class에 method를 구현하기 ⇒ 참고 “class 생성과 상속”
abstract class IronMan {
String name;
String suitColor;
IronMan(this.name, this.suitColor);
void fly();
void shootLasers();
void withstandDamage();
}
// IronMan class 생성
class Mark50 extends IronMan {
int height;
Mark50(String name, String suitColor, this.height): super(name, suitColor);
void fly() {
print('$name is flying');
}
void shootLasers() {
print('$name is shooting');
}
void withstandDamage() {
print('$name is defensing');
}
}
// IronMan class를 상속받은 Mark50 클래스 생성
void main () {
var mark50 = Mark50('Mark50', 'red', 30);
mark50.fly();
mark50.shootLasers();
mark50.withstandDamage();
}
//함수 실행
👉 그럼 추상클래스는 왜 쓸까?
⇒ 이는 코드의 가독성과 유지보수의 기능을 향상시킴
아래 블로그를 참고했다. 감사합니다 😀
https://jinhan38.com/119