Last active
November 20, 2019 01:35
-
-
Save juanbecerra0/bc3949ba0d53927346f11104c1a9af2d to your computer and use it in GitHub Desktop.
This file contains 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
// Abstract class that other classes can implement | |
abstract class Item { | |
use(); | |
} | |
// Inherits the 'Item' class's 'use()' method | |
class Box<T> implements Item { | |
// ADT list that can contain other objects | |
List<T> contents; | |
Box(this.contents); | |
use() => print("$this has ${contents.length} items in it."); | |
} | |
// Inherits the 'Item' class's 'use()' method | |
class Ball implements Item { | |
int value = 0; | |
use() => print("$this is worth \$$value."); | |
} | |
// Child of 'Ball' with different 'value' integer | |
class RedBall extends Ball { | |
int value = 5; | |
} | |
// Child of 'Ball' with different 'value' integer | |
class BlueBall extends Ball { | |
int value = 10; | |
} | |
// Child of 'Ball' with different 'value' integer | |
class GreenBall extends Ball { | |
int value = 20; | |
} | |
main() { | |
// Instance of 'Box' with list type 'Item' | |
var b = Box<Item>([ | |
RedBall(), | |
BlueBall(), | |
GreenBall() | |
]); | |
// Prints the contents length of box | |
b.use(); | |
// Prints the price of each object within the box's list | |
for (var item in b.contents) { | |
item.use(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment