Time for action – A fridge with constraints

Therefore, now our fridge will only be able to access eatable items.

  1. First, create a parameterized Fridge class that will only accept types implementing the Eatable interface:
    class Fridge<T: Eatable)>
    {
       public function new()
       {}
    }
  2. In this class, we will create two methods: an add and a remove method and we will use an Array to store the list of what is inside our Fridge:
    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.