Skip to content

Instantly share code, notes, and snippets.

@sho-ito-1027
Created June 23, 2020 11:48
Show Gist options
  • Save sho-ito-1027/9d5c94e9dbd142cf1758a4bbefcb567b to your computer and use it in GitHub Desktop.
Save sho-ito-1027/9d5c94e9dbd142cf1758a4bbefcb567b to your computer and use it in GitHub Desktop.
Flutter勉強会3回目の問題(抽象化クラス)
void main() {
Impreza1 car1 = Impreza1();
Impreza2 car2 = Impreza2();
Impreza3 car3 = Impreza3();
Impreza4 car4 = Impreza4();
}
abstract class Car {
String name;
void accelerate();
void stop();
}
// 抽象化クラスのサブクラス。
// fieldに関しては抽象化クラスで定義が足りているので省略可能?
// そのため、具象化するために抽象化クラスの定義したものを実装する必要がある
// ① OK
class Impreza1 extends Car {
void accelerate() {}
void stop() {}
}
// void accelerate();がないので当然ダメ
// ② NG
class Impreza2 implements Car {
String name = 'Impreza2';
void stop() {}
}
// 全部揃ってるのでOK
// ③ OK
class Impreza3 implements Car {
String name = 'Impreza3';
void accelerate() {}
void stop() {}
}
// 抽象化クラスのサブクラス。
// 抽象化クラスの定義したメソッドは実態がない。
// そのため、具象化するために抽象化クラスの定義したものを実装する必要がある
// ④ OK
class Impreza4 extends Car {
String name = 'Impreza4';
void accelerate() {}
void stop() {}
void soundHorn() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment