03 Objects
-
Upload
thewhiteafrican -
Category
Education
-
view
52 -
download
0
description
Transcript of 03 Objects
How Objects Can Change Your Life.
The Setup: Two programmers, Larry and Brad, are given a challenge by their project manager to build a program that meets the following specifications. First to finish wins an extra weeks vacation.
SPECThere will be shapes on a GUI, a square, a circle, and a triangle. When the user clicks on a shape, the shape will rotate clockwise 360° and play an AIF sound file specific to that particular shape.
Larry’s Cube(procedural approach)
Brad’s Café(OOP approach)
rotate (shapeNum) {// make the shape
rotate // 360°
}
playSound (shapeNum) {//use shapeNum to
lookup // which AIF sound to
play, //and play it
}
rotate (shapeNum) {// make the shape
rotate // 360°
}
playSound (shapeNum) {//use shapeNum to
lookup // which AIF sound to
play, //and play it
}
Squarerotate ( ) {
// code to rotate a //square
}playSound ( ) {
// code to play the // AIF file for a // square
}
rotate ( ) {// code to// rotate a triangle
}playSound ( ) {
// code to play the // AIF file for a
triangle}
Triangle
rotate ( ) {// code to rotate a //circle
}playSound ( ) {
// code to play the // AIF file for a // circle
}
Circle
Which do you think is better?
Hold the Press! There’s a spec change.
SPEC ChangeThere will be an amoeba shape on the screen, with the others. When the user clicks on the amoeba, it will rotate like the others, and play a .hif sound file.
Larry’s Cube(procedural approach)
Brad’s Café(OOP approach)
rotate (shapeNum) {// make the shape
rotate // 360°
}
playSound (shapeNum) { //if the shape is not an
//amoeba, use shapeNum
//to lookup which AIF //sound to play, and play it //else
//play amoeba .hif sound}
rotate (shapeNum) {// make the shape
rotate // 360°
}
playSound (shapeNum) { //if the shape is not an
//amoeba, use shapeNum
//to lookup which AIF //sound to play, and play it //else
//play amoeba .hif sound}
Squarerotate ( ) {
// code to rotate a //square
}playSound ( ) {
// code to play the // AIF file for a // square
}
rotate ( ) {// code to// rotate a triangle
}playSound ( ) {
// code to play the // AIF file for a
triangle}
Triangle
rotate ( ) {// code to rotate a //circle
}playSound ( ) {
// code to play the // AIF file for a // circle
}
Circle
rotate ( ) {// code to rotate a //circle
}
playSound ( ) {// code to play the // new .hif file for an// amoeba
}
Amoeba
Why is it not a good idea to edit previously tested code?
What the Spec Forgot to Mention…
SPEC ChangeAmoeba rotation point in Larry and Brad’s version.
Where the amoeba rotation point should be.
Larry’s Cube(procedural approach)
Brad’s Café(OOP approach)
rotate (shapeNum, xPt, yPt) { //if the shape is not an amoeba,
//calculate the center point //based on a rectangle, then //rotate.
//else //use the xPt and yPt as the //rotation point offset and //then rotate.
}
playSound (shapeNum) { //if the shape is not an
//amoeba, use shapeNum //to lookup which AIF
//sound to play, and play it //else
//play amoeba .hif sound}
rotate (shapeNum, xPt, yPt) { //if the shape is not an amoeba,
//calculate the center point //based on a rectangle, then //rotate.
//else //use the xPt and yPt as the //rotation point offset and //then rotate.
}
playSound (shapeNum) { //if the shape is not an
//amoeba, use shapeNum //to lookup which AIF
//sound to play, and play it //else
//play amoeba .hif sound}
int xPoint;int yPoint;
rotate ( ) {//code to rotate an amoeba //using amoeba’s x and y
}
playSound ( ) {// code to play the // new .hif file for an// amoeba
}
Amoeba
Which approach is more efficient? Changing one class or editing the entire file?
SO did Brad win the vacation?
Well Larry didn’t go down without a fight and pointed out that Brad’s code had a lot of duplicated code in it…???
Ahaa but did Larry see Brad’s final design?This is what Brad
did…Square
rotate ( ) playSound ( )
rotate ( ) playSound ( )
Triangle
rotate ( ) playSound ( )
Circle rotate ( )
playSound ( )
AmoebaLarry looked at what all four classes had in common.
1
2
They’re all shapes, and they all rotate and playSound. So Larry abstracted out the common features and put them into a new class called Shape.
Shape
rotate ( )playSound ( )
3
Then Larry linked the other four classes to the new Shape class, in a relationship called inheritance.
Shape
rotate ( )playSound ( )
superclass
subclasses
You can read this as, “Square inherits from Shape”, “Circle inherits from Shape”, and so on. rotate() and playSound() have been taken out of all other shapes and replaced by one copy in a
superclass called Shape. The other four are the
subclasses of Shape. The subclasses inherit the
methods of the superclass.
Square Trian
gle
Circle Amoeba
What about the Amoeba rotate()?Wasn’t that the whole problem here – that the amoeba shape had a
completely different rotate and playSound procedure? How can Amoeba do something different if it “inherits” its functionality from the Shape class??? Shape
rotate ( )playSound ( )
Square Trian
gle
Circle
rotate ( ) {// code to rotate a //circle
}
playSound ( ) {// code to play the // new .hif file for an// amoeba
}
Amoeba
4
The Amoeba class
overrides the methods of the Shape class. Then at runtime, the JVM knows exactly which rotate() method to run when someone tells the Amoeba to rotate.
Overriding just means that a subclass redefines one of its inherited methods when it needs to change or extend the behavior of that method.
Overriding methods
Back to ClassesWhen you design a class you need to think about the objects that will be created from that class and the:
• things the object KNOWS• things the object DOES
Shopping CartcartContents
addToCart()removeFromCart()checkCart()
knows
does
Button
labelcolor
setColor()setLabel()pressOn()pressOff()
knows
does
Alarm
alarmTimealarmMode
getAlarmTime()setAlarmTime()isAlarmSet()snooze()
knows
does
Remember a class is the blueprint for an object!
Things an objects knows about itself are called
• instance variables
Things an object can do are called• methods
Song
knows
does
instance variables (state)
methods (behavior)
What could go here?
What could go here?
Why are they called “instance” variables?
What does “state” mean?
Making Your First Object
Write your class
1 class Dog {int size;String breed;String name;
void bark() {System.out.println(“Woof!
Woof!”);}
}
Dog
sizebreedname
bark()
instance variables
methods
Write a tester (TestDrive) class
2 class DogTestDrive {public static void main (String [ ]
args) {//Dog test code goes here
}}
Making Your First Object
In your test, make an object and access the object’s variables and methods
3 class DogTestDrive {public static void main (String [ ]
args) {
Dog rufus = new Dog();
rufus.size = 40;rufus.breed = “cockapoo”;rufus.name = “Rufus”;
rufus.bark();}
}
rufus
size = 40breed = cockapooname = Rufus
bark()
Make a new Dog object named “rufus”
Use the dot operator (.) to set the size, breed, and name of rufus.
Use the dot operator (.) to call the rufus’ bark() method.
If you already have some OOP experience, you might notice we are not using encapsulation. We’ll get to that in chapter 4 of the text.
Another Example of Building a Class and Testing it.
class Movie {String title;String genre;int rating;
void playIt() {System.out.println(
“Playing the movie”);
}}
Why is there no main()?
public class MovieTestDrive {public static void main(String [ ]
args) {Movie one = new Movie();one.title = “Gone with the
Stock”;one.genre = “Tragic”;one.rating = -2;
Movie two = new Movie();two.title = “Lost in Cubicle
Space”;two.title = “Comedy”;two.rating = 5;two.playIt();
Movie three = new Movie();three.title = “Byte Club”;three.genre = “Tragic but
ultimately uplifting”;three.rating = 100;
}}clas
stester