IAT 265 OO programming - Inheritance
description
Transcript of IAT 265 OO programming - Inheritance
IAT 265 1
______________________________________________________________________________________
SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW.SIAT.SFU.CA
IAT 265OO programming - Inheritance
IAT 265 2
Outlineg Object-oriented programming
– objects– classes
• sets (mutators) and gets (accessors)• object methods
– Inheritance: Subclasses• Rocket, ArmedRocket
g Collections– ArrayList
Jun 13, 2014, 2014
IAT 265 3
Inheritance
Jun 13, 2014, 2014
Classesg Types
– Primitives: int, float, char, boolean …– Objects: array, string, class …
Jun 13, 2014, 2014 IAT 265
Objectsg We’ve worked with some objects
before, like Arrays.g We can make our own objects, to
keep related data together, with methods to control that data.
Jun 13, 2014, 2014 IAT 265
Classesg Classes are the blueprints for our
new objects.g To declare a new Class (a new type
of object):
Jun 13, 2014, 2014 IAT 265
class MyToy { // fields (class variables) // methods (class functions)}
Fields and Methods
Jun 13, 2014, 2014 IAT 265
class MySquare { int xPos, yPos; MySquare(x, y) {
xPos = x;yPos = y;
}
void drawMe() {rect(xPos, yPos, 50, 50);
}}
x y
drawMe()
fields
constructor
methods
(one kind of method)
Fields and Methods
Jun 13, 2014, 2014 IAT 265
class MySquare { int xPos, yPos;
MySquare(x, y) {xPos = x;yPos = y;
}
void drawMe() {rect(xPos, yPos, 50, 50);
}}
x y
drawMe()
MySquare square1 = new MySquare(10, 10);MySquare square2 = new MySquare(20, 90);
10 10
drawMe()
20 90
drawMe()
square1 square2
Fields and Methods
Jun 13, 2014, 2014 IAT 265
class MySquare { int xPos, yPos;
MySquare(int x, int y) {xPos = x;yPos = y;
}
void drawMe() {rect(xPos, yPos, 50, 50);
}}
MySquare square1 = new MySquare(10, 10);MySquare square2 = new MySquare(20, 90);
x y
drawMe()
10 10
drawMe()
20 90
drawMe()
square1 square2
square1.drawMe();square2.drawMe();
Arrays of Objects?g Let’s make a bunch of squares!
Jun 13, 2014, 2014 IAT 265
MySquare[] squares = new MySquare [10] ;
// initialize all of our squares.for (int i = 0; i < 10; i ++) { squares[i] = new MySquare(i*10, i*10);}
squares[4].drawMe(); // draw the 4th square.
Recap: Rocketg Earlier, we created the Rocket class
– Constructor: Rocket(int initialX, int initialY, float initialRot )
– Methodsdraw()rotateClockwise()rotateCounterClockwise()fireThrusters()
Jun 13, 2014, 2014 IAT 265
Asteroidsg Let’s adapt this to make an array of
Asteroids for our Rocket
Jun 13, 2014, 2014 IAT 265
class Asteroid { //fields float rotation = 0; float xPos, yPos; float velocityX, velocityY; long lastDrawMillis = 0; …}
Asteroidsg When we create an asteroid, let’s
have it start in a random position, and move in a random direction.
Jun 13, 2014, 2014 IAT 265
class Asteroid { … // constructor Asteroid() {
xPos = random(0, 400);yPos = random(0, 400);rotation = random(0, TWO_PI);velocityX = sin(rotation)*10;velocityY = -cos(rotation)*10;
}
Asteroids
Jun 13, 2014, 2014 IAT 265
class Asteroid { … // draw method void draw () {
Revisit our exampleg So far we have a rocket that flies around
in a field of asteroids
g What if we want our rocket to be able to fire – But we don’t want to get rid of our non-firing
rocket
g Create a subclass!Jun 13, 2014, 2014 IAT 265
Inheritanceg Subclasses inherit fields and
methods from parent
class ArmedRocket extends Rocket {…
}
Jun 13, 2014, 2014 IAT 265
Our subclass needs a constructorg Our empty ArmedRocket example creates an error
– Processing doesn’t know how to construct an ArmedRocket
g We want the ArmedRocket constructor to do the same work as the Rocket constructor
ArmedRocket(int initialX, int initialY, float initialRot) {
super(initialX, initialY, initialRot); }
The keyword super means to refer to the parent class.
In this case, to call the Parent Class ConstructorJun 13, 2014, 2014 IAT 265
Now we have ArmedRocketg We can use an ArmedRocket now in
our example
g But, it’s basically just a copy of Rocket
g The only reason to define an ArmedRocket is to add new capabilities or to override old ones
Jun 13, 2014, 2014 IAT 265
Add a fire() method g We want our fire method to draw a
missile that shoots out of the rocket
g We could have the fire method draw the missile…– Is there a problem with this?
Jun 13, 2014, 2014 IAT 265
Missiles should also be objectsg The object oriented solution is to make the
missile an object as well– All the different types of “things” in our domain
should have a corresponding class
g Like asteroids and rockets, the missile class should know how to draw itself– A Missile is similar to a rocket (position, rotation,
draw method, etc.)
g Now our ArmedRocket.fire() method can just create and return a missile
Jun 13, 2014, 2014 IAT 265
The fire() methodMissile fire() { Missile m = new Missile(xPos, yPos, rotation);
return m;}
g Now add code in loop to draw missilesJun 13, 2014, 2014 IAT 265
Missiles destroy asteroidsg So far we have a rocket that flies around
in a field of asteroids and fires
g Now we want our missiles to blow up asteroids– This means we need a variable number of
asteroids. – How do we do this with an array?– Use an ArrayList!– Also need to figure out when we have a
collisionJun 13, 2014, 2014 IAT 265 22
The Java SDK g Java comes with thousands of
classes in the Java Platform API
g Documentation is available on Sun’s website
g Let’s look at ArrayList
Jun 13, 2014, 2014 IAT 265 23
ArrayList g It’s a resizeable list
– Can add and delete things without worrying about declaring the size
g The main methods we care about are add(), get(), and remove(), and size()
g Steps in using ArrayList– Declare a variable of type ArrayList– Create a new ArrayList and assign it to the variable– Call add(), get() and remove() and size() on ArrayList as you
need them
Jun 13, 2014, 2014 IAT 265 24
Parents and childreng Remember that we declared a child class ArmedRocket whose
parent was Rocketg Remember that classes are types
– So ArmedRocket is a type and Rocket is a type
g So, here are some legal assignments– ArmedRocket r1 = new ArmedRocket(50, 60, 0);– Rocket r2 = new Rocket(50, 60, 0); – Rocket r3 = new ArmedRocket(50, 60, 0);
g But this is illegal– ArmedRocket r4 = new Rocket(50, 60, 0);
g Same goes for method arguments as well…Jun 13, 2014, 2014 IAT 265 25
Rocket Inheritance
Jun 13, 2014, 2014 IAT 265 26
Rocket:xPos, YPos, velocityX, velocityY, rotation
Rocket(x,y,rotation) draw()
ArmedRocket extends RocketxPos, YPos, velocityX, velocityY, rotationArmedRocket(x,y,rotation) draw() fire()
Inherits from
Using ArrayList.add()g The argument type of the add method is
Object– Object is the parent class of all classes– With an object argument type, you can pass in
an object of any class
g So, to initialize our asteroids… ArrayList asteroids = new ArrayList(); for(int i = 0; i < numAsteroids; i++) asteroids.add(new Asteroid());
Jun 13, 2014, 2014 IAT 265 27
Getting things out of an ArrayListg ArrayList.get(int i) – returns the
ith object (starting with 0)
g But this doesn’t work!asteroids.get(i).draw();Why?
Jun 13, 2014, 2014 IAT 265 28
Need to cast back from Objectg Since things are put in an ArrayList as
Object, they come back out as Object– It’s like they forget their more detailed type– So, when using ArrayList (or any container
class), need to cast back to the more detailed type
Asteroid asteroid = (Asteroid)asteroids.get(i);
if (!asteroid.collision(r1)) asteroid.draw();Jun 13, 2014, 2014 IAT 265 29
Pushing collision detection into the Asteroid
g In the current code, detecting collision takes place in loop()g But it is cleaner (more object-oriented) if Asteroid itself
knows how to detect collision– Detecting collision depends on knowing the boundaries of the
asteroid, which properly belongs in the asteroid class
boolean collision(Rocket r) { if ((r.xPos >= xPos - 26 && r.xPos <= xPos + 22) && (r.yPos >= yPos - 24 && r.yPos <= yPos + 26)) return true; else return false; }
Jun 13, 2014, 2014 IAT 265 30
Destroying asteroidsg When a missile hits an Asteroid, we need to
destroy it– This was the whole reason for using ArrayList– Big asteroids turn into two small asteroids– Small asteroids disappear
void destroy(ArrayList asteroids) { asteroids.remove(this); if (large) { asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); asteroids.add(new Asteroid(false, xPos, yPos, lastDrawMillis)); } }
Jun 13, 2014, 2014 IAT 265 31
Super and thisg this is a special variable that always refers to
the current instance (object)– Useful in methods to refer to yourself– this.method() – calls a method on yourself (but normally
you just directly call the method)– this() – calls a constructor on yourself (useful for one
version of a constructor to call another)
g super is a special variable that always refers to the superclass portion of an object (the object cast into it’s superclass)– super.method() – calls the superclass’s method– super() – calls the superclass’s constructor
Jun 13, 2014, 2014 IAT 265 32
Summaryg ArrayList, a Java Platform collection class
g Learned about super and subclasses as types– Any instance of a subclass is an instance of the
superclass, but not visa-versa– Can cast more abstract classes (parents) into more
concrete classes (children)
g The Java keywords super and this – Special variables that can be used within a method
to refer to yourself (the superclass portion of yourself and all of yourself)
Jun 13, 2014, 2014 IAT 265 33