Time for action – A fridge with constraints
Therefore, now our fridge will only be able to access eatable items.
- First, create a parameterized
Fridge
class that will only accept types implementing theEatable
interface:class Fridge<T: Eatable)> { public function new() {} }
- In this class, we will create two methods: an
add
and aremove
method and we will use anArray
to store the list of what is inside ourFridge
:class Fridge<T: Eatable)> { private var items : Array<T>;public function new() { items = new Array<T>(); //Initialize the Array } public function add(item : T) { array.push(item); } public function remove(item : T) { array.remove(item); } }
For sure, in this state, our fridge is basically just a wrapper around the array class, but now we ...
Get haXe 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.