Method override & super kewword
void main() {
// Method overriding
Parent parent = new Parent(3);
Child child = new Child(3);
print('parent.cal():' + parent.cal().toString());
print('child.cal():' + child.cal().toString());
}
class Parent {
final int _number;
Parent(this._number);
int cal() {
return this._number * this._number;
}
}
class Child extends Parent {
Child(int number) : super(number);
@override
int cal(){ # @ 키워드를 부모의 함수를 재정의
return super.cal() * 2; # super 키워드를 이용해서 부모의 함수를 사용
}
}
--------------------------------------------[result]
parent.cal():9
child.cal():18
'Dart' 카테고리의 다른 글
[Dart] Class - super, this (0) | 2021.05.27 |
---|---|
[Dart] Class - Static (0) | 2021.05.27 |
[Dart] Class - 상속/Inheritance (0) | 2021.05.27 |
[Dart] Class - getter, setter (0) | 2021.05.27 |
[Dart] Class - 선언 및 생성자 (0) | 2021.05.27 |