Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 will do something interesting with it.

  3. We will define the Eatable interface, so that we can store the date before which the eatable should be eaten, as follows:

    interface...