* 상속
class Spacecraft {
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');
}
}
}
class Orbiter extends Spacecraft {
num altitude;
Orbiter(String name, DateTime launchDate, this.altitude)
: super(name, launchDate);
}
void main() {
Orbiter ob = Orbiter("히드라", DateTime.now(), 50);
ob.describe();
}
//결과
Spacecraft: 히드라
Launched: 2021 (0 years ago)
+ 추가 상속(mixin) : with 이용. 여러 클래스 계층에서 클래스의 코드를 재사용하는 방법
class Work {
var money;
}
class Student {
var year;
}
class School {
var name = "elementary school";
}
class People extends Work with Student {}
class People2 extends Work with Student, School {}
void main() {
People p = People();
p.year = 2019;
p.money = 20000;
print(p.year);
print(p.money);
People2 p2 = People2();
print(p2.name);
}
//결과
2019
20000
elementary school
같이 보면 좋은 포스팅
https://cording-cossk3.tistory.com/84
https://cording-cossk3.tistory.com/85
https://cording-cossk3.tistory.com/86
https://cording-cossk3.tistory.com/87
https://cording-cossk3.tistory.com/88
320x100
'프로그래밍 > Flutter-Dart' 카테고리의 다른 글
[Flutter] Dart 문법 (async, Future, await, then) (0) | 2021.03.15 |
---|---|
[Flutter] Dart 문법 (abstract, implement) (0) | 2021.03.15 |
[Flutter] Dart 문법 (class, ?, !, ?.) (0) | 2021.03.15 |
[Flutter] Dart 문법 (변수, 함수, 화살표 함수, 조건문, 반복문) (0) | 2021.03.15 |
[Flutter] flutter 윈도우 설치 (0) | 2021.03.15 |
댓글