Dart
[Dart] Class - Method override
unsungIT
2021. 5. 27. 20:19
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