* 추상클래스, 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
[Flutter] Dart 문법 (상속)
* 상속 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 = "무탈리스크"; launc
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/84
[Flutter] Dart 문법 (abstract, implement)
* 추상클래스, implement : 추상클래스 및 일반 클래스 implement 가능. - implement하면 클래스가 구현해 둔 메서드도 재정의 해야할 강제성이 생김. (일반 메서드도 구현해야 함) class Spacecraft extends Describ
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/85
[Flutter] Dart 문법 (async, Future, await, then)
* 비동기 - 아래 printWithDelay함수에서 await를 빼고 실행시키면 Future.delayed와 print가 동시에 실행되는 듯 하고, await을 넣고 실행시키면 delay가 적용된 후에 print가 실행된다. //Future : An object representing
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/86
[Flutter] Dart 문법 (getter, setter)
* getter, setter class Rectangle { double left, top, width, height; Rectangle(this.left, this.top, this.width, this.height); //Define two calculated properties : right and bottom. double get right => left + width; set right(double value) => left = value -
cording-cossk3.tistory.com
https://cording-cossk3.tistory.com/87
[Flutter] Dart 문법 (json)
* 클래스 직렬화 (Json) - Flutter는 간단한 json 인코더와 디코더가 내장된 dart:convert 라이브러리 가짐 - fromJson : map 구조에서 새로운 User 객체 생성하기 위한 생성자 - toJson : User 객체를 map 구조로 변
cording-cossk3.tistory.com
'프로그래밍 > 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 |
댓글