Skip to content

Instantly share code, notes, and snippets.

@aryzae
Last active June 23, 2020 09:25
Show Gist options
  • Save aryzae/b3182491b96623bbb38c616fbf65c5c5 to your computer and use it in GitHub Desktop.
Save aryzae/b3182491b96623bbb38c616fbf65c5c5 to your computer and use it in GitHub Desktop.
Flutter勉強会3回目の資料(継承)
void main() {
Animal animal1 = Mike();
print(animal1.breed);
animal1.bark();
Animal animal2 = Shiba();
print(animal2.breed);
animal2.bark();
Animal animal3 = Stranger();
print(animal3.breed);
animal3.bark();
Animal animal4 = Unknown();
print(animal4.breed);
animal4.bark();
}
class Animal {
// field
String breed = 'Animal';
// method
void bark() {
print('I am Animal.');
}
}
class Mike extends Animal {
@override
String breed = 'Cat';
@override
void bark() {
print('meow');
}
}
class Shiba extends Animal {
get breed => 'Dog';
set breed(String newValue) => breed = newValue;
// overrideがなくても問題ない
void bark() {
print('bow-wow');
}
}
class Stranger extends Animal {
String breed = 'Human?';
void bark() {
// overrideがなくてもsuperを呼べる
super.bark();
}
}
class Unknown extends Animal {
// overrideしなければ親の値が自動的に呼ばれる
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment