Last active
June 23, 2020 10:25
-
-
Save aryzae/f21ccb8644f67543abcae880762198ed to your computer and use it in GitHub Desktop.
Flutter勉強会3回目の資料(Mixin)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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