O'Reilly logo

haXe 2 by Benjamin Dasnois

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required