Skip to content

Instantly share code, notes, and snippets.

@aryzae
Last active June 23, 2020 10:25
Show Gist options
  • Save aryzae/f21ccb8644f67543abcae880762198ed to your computer and use it in GitHub Desktop.
Save aryzae/f21ccb8644f67543abcae880762198ed to your computer and use it in GitHub Desktop.
Flutter勉強会3回目の資料(Mixin)
void main() {
Owl owl = Owl();
print(owl.canFly);
owl.fly();
print(owl.canRun);
owl.run();
Dog dog = Dog();
print(dog.canRun);
dog.run();
}
mixin RunAbility {
bool canRun = true;
void run() {
print('run run ru-');
}
}
mixin FlyAbility on Bird {
bool canFly = true;
void fly() {
print('I can flyyyyyyy!!!');
}
}
abstract class Animal {
String breed;
void bark();
}
class Bird implements Animal {
String breed = 'Bird';
void bark() {
print('cheep-cheep');
}
}
// FlyAbilityはBird型の制約がついているのでBird型のsubclassにしか適用できない
class Owl extends Bird with FlyAbility, RunAbility {
String breed = 'Owl';
void bark() {
print('Tu-wit tu-whoo');
}
}
class Dog extends Animal with RunAbility {
String breed = 'Dog';
void bark() {
print('bow-wow');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment