Last active
February 23, 2020 11:51
-
-
Save digitaljoni/965b9e240a01326945424fb8244592e3 to your computer and use it in GitHub Desktop.
OOP DBZ - Fight Method
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
| class Saiyan { | |
| Saiyan({this.name, this.power}); | |
| Saiyan.superForm(this.name) : power = 10000; | |
| final String name; | |
| final int power; | |
| Saiyan operator +(Saiyan otherSaiyan) { | |
| return Saiyan( | |
| name: '$name${otherSaiyan.name}', | |
| power: power + otherSaiyan.power, | |
| ); | |
| } | |
| Saiyan fight(Saiyan otherSaiyan) { | |
| final Saiyan winner = (power > otherSaiyan.power) ? this : otherSaiyan; | |
| return winner; | |
| } | |
| @override | |
| String toString() => 'Saiyan $name Power Level : $power'; | |
| } | |
| void main() { | |
| final Saiyan goku = Saiyan(name: 'Goku', power: 8000); | |
| print(goku); | |
| final Saiyan vegeta = Saiyan(name: 'Vegeta', power: 6000); | |
| print(vegeta); | |
| final Saiyan broly = Saiyan.superForm('Broly'); | |
| print(broly); | |
| final Saiyan gogeta = goku + vegeta; | |
| final Saiyan winner = gogeta.fight(broly); | |
| print('Winner is ${winner.name}'); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment