본문 바로가기
프로그래밍/Flutter-Dart

[Flutter] Dart 문법 (상속)

by 채연2 2021. 3. 15.

 

 

 

* 상속


		
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

 

[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

https://cording-cossk3.tistory.com/88

 

[Flutter] Dart 문법 (skip, take)

* skip, take - skip : 첫 번째 (count) 요소 제외한 모든 요소 제공하는 iterable 반환, 즉 count개 요소 제외한 모든 요소 - take : 이 iterable의 첫 번째 요소 (count) 개의 lazy iterable 반환, 즉 iterable의 첫 번째 요

cording-cossk3.tistory.com

 

320x100

댓글