* 추상클래스, implement : 추상클래스 및 일반 클래스 implement 가능.
- implement하면 클래스가 구현해 둔 메서드도 재정의 해야할 강제성이 생김. (일반 메서드도 구현해야 함)
class Spacecraft extends Describable {
String? name;
DateTime? launchDate;
//Constructor, with syntactic sugar for assignment to members.
Spacecraft(this.name, this.launchDate) {
//Initialization code goes here.
}
Spacecraft.origin() {
name = "무탈리스크";
launchDate = DateTime.now();
}
//Named contructor that forwards to the default one.
Spacecraft.unlaunched(String name) : this(name, null);
int? get launchYear => launchDate?.year; //read-only non-final property
//Method
void describe() {
print('Spacecraft: $name');
if (launchDate != null) {
int years = DateTime.now().difference(launchDate!).inDays ~/ 365;
print('Launched: $launchYear ($years years ago)');
} else {
print('Unlaunched');
}
}
}
abstract class Describable {
void describe();
void describeWithEmphasis() {
print('=========');
describe();
print('=========');
}
}
//일반 클래스 implements시, 일반 메서드도 다 구현
class MyClass implements Spacecraft {
@override
DateTime? launchDate;
@override
String? name;
MyClass(this.name);
@override
void describe() {
print('#### Spacecraft: $name / Unlaunched ####');
}
@override
void describeWithEmphasis() {}
@override
int? get launchYear => throw UnimplementedError();
}
void printShow(Spacecraft spacecraft) => spacecraft.describe();
void main() {
Spacecraft sc = Spacecraft("스카우트", DateTime.now());
sc.describeWithEmphasis();
printShow(MyClass("ABC"));
}
//결과
=========
Spacecraft: 스카우트
Launched: 2021 (0 years ago)
=========
#### Spacecraft: ABC / Unlaunched ####
같이 보면 좋은 다른 포스팅
https://cording-cossk3.tistory.com/83
https://cording-cossk3.tistory.com/84
https://cording-cossk3.tistory.com/85
https://cording-cossk3.tistory.com/86
https://cording-cossk3.tistory.com/87
320x100
'프로그래밍 > Flutter-Dart' 카테고리의 다른 글
[Flutter] Dart 문법 (getter, setter) (0) | 2021.03.15 |
---|---|
[Flutter] Dart 문법 (async, Future, await, then) (0) | 2021.03.15 |
[Flutter] Dart 문법 (상속) (0) | 2021.03.15 |
[Flutter] Dart 문법 (class, ?, !, ?.) (0) | 2021.03.15 |
[Flutter] Dart 문법 (변수, 함수, 화살표 함수, 조건문, 반복문) (0) | 2021.03.15 |
댓글