28.04.2019 Views

[JAVA][Beginning Java 8 Games Development]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10 ■ Directing the Cast of Actors: Creating a Casting Director Engine and Creating the Bagel Actor Class<br />

So, if you had defined a Hero interface, and wanted to implement it in one of your classes, in which case the <strong>Java</strong><br />

compiler would watch over the code and make sure that you are implementing the necessary method structures, the<br />

class definition line of code and the methods inside the body of the class would look something like the following:<br />

public class SuperHero implements Hero {<br />

protected boolean flagVariable1, flagVariable2;<br />

public void update() {<br />

// <strong>Java</strong> statements to process on each update<br />

}<br />

public boolean collide(Actor actor) {<br />

// <strong>Java</strong> statements to process for collision detection<br />

}<br />

}<br />

210<br />

So with the java.util ArrayList class that we looked at earlier, the technical class definition is as follows:<br />

public class ArrayList extends AbstractList implements List<br />

The ArrayList class also implements RandomAccess, Cloneable, and Serializable, but we will not be using<br />

those at this time so I am just showing you the parts of the ArrayList class definition that pertain to what we will<br />

be learning during this chapter, not the full public class ArrayList extends AbstractList implements<br />

List, RandomAccess, Cloneable, Serializable class definition, as you would see if you look at the <strong>Java</strong> class<br />

documentation for the ArrayList class online.<br />

It is important to notice that the .addAll(), .removeAll() and .clear() method calls that we will use with the<br />

ArrayList class are implemented because the List <strong>Java</strong> interface demands that they be implemented, so that is<br />

the connection between the classes and why we will specify the declaration of the ArrayList object using this code:<br />

private List CURRENT_CAST = new ArrayList();<br />

You may be wondering why we will not need to explicitly specify the Actor object type on both sides of this<br />

declaration and instantiation statement. Prior to <strong>Java</strong> 7, you would have needed to specify your Actor object type on<br />

both sides of this statement, inside of the ArrayList() constructor method call. So, if you are writing game code that<br />

needs to be compatible with <strong>Java</strong> 5 and <strong>Java</strong> 6, you would code this statement using the following line of <strong>Java</strong> code:<br />

private List CURRENT_CAST = new ArrayList();<br />

Now that we have learned what a <strong>Java</strong> interface is, let’s take a look at the List public interface in detail.<br />

The List Public Interface: A List Collection of <strong>Java</strong> Objects<br />

The List public interface is also a member of the <strong>Java</strong> Collections Framework. The <strong>Java</strong> public interface List<br />

extends the Collections public interface, which extends the Iterable public interface. Thus, the super interface<br />

to sub interface hierarchy would look something like this following List <strong>Java</strong> interface hierarchy:<br />

Interface Iterable<br />

> Interface Collection<br />

> Interface List<br />

A List is an ordered Collection and could also be thought of as a sequence of objects. In our case, the<br />

List will be an ordered sequence of Actor objects. A user of the List interface has precise control over where in<br />

the List each element (in our case, Actor object) is inserted. The user can access elements using an integer index, that<br />

is, the position in the List, using a parenthesis after the name of the List. You can also search for elements in a List.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!