Last active
February 23, 2020 12:14
-
-
Save digitaljoni/1856b6fc5c62ce9cd99ede05b2e071c3 to your computer and use it in GitHub Desktop.
DBZ - Operator Overloading/Overriding
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, | |
| ); | |
| } | |
| @override | |
| String toString() => 'Saiyan $name Power Level : $power'; | |
| } | |
| void fight(Saiyan saiyan1, Saiyan saiyan2) { | |
| if (saiyan1.power > saiyan2.power) { | |
| print('${saiyan1.name} wins'); | |
| } else { | |
| print('${saiyan2.name} wins'); | |
| } | |
| } | |
| 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; | |
| fight(gogeta, broly); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment