IAT 265 Objects
description
Transcript of IAT 265 Objects
IAT 265 1
______________________________________________________________________________________
SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW.SIAT.SFU.CA
IAT 265Objects
IAT 265 2
Outlineg Object-oriented programming
– Object components– Rocket– Primitive types and Object References
g Objects, another metaphorg Why objects?
Jun 9, 2014
IAT 265 3
Classes vs Objectsg A Class is a blueprint for a bicycleg An Object is a bicycle
g Many bicycles, one blueprint
Jun 9, 2014
Jun 9, 2014 IAT 265 4
Parts of a classg Classes define fields, constructors and methods
g Fields are the variables that will appear inside every instance of the class– Each instance has its own values
g Constructors are special methods that define how to build instances (generally, how to set the initial values of fields)
g Methods are how you do things to instances
Jun 9, 2014 IAT 265 5
Defining the rocket classclass Rocket {
// fieldsfloat rotation = 0;
float xPos; float yPos; final int halfWidth = 10; final int halfHeight= 10;
// constructorRocket( int initialX, int initialY,
float initialRot ) {
xPos = initialX; yPos = initialY;rotation =
initialRot;}
void draw() { pushMatrix(); translate(xPos, yPos); rotate(rotation);
triangle(0, -halfHeight, -halfWidth, halfHeight, halfWidth, halfHeight);
rectMode(CORNERS); rect(-halfWidth + 5, halfHeight, -halfWidth + 8, halfHeight + 3); rect(halfWidth - 8, halfHeight, halfWidth - 5, halfHeight + 3);
popMatrix(); }
}
Jun 9, 2014 IAT 265 6
Using the class to create instances
g Classes define a typeg You can now declare variables of this type and initialize
them using the constructorg Like arrays, the keyword new is used to tell Java to create a
new object
Rocket r1, r2 ;void setup() {
r1 = new Rocket(75, 10, 0);r2 = new Rocket(50, 50, PI/2);
}void draw() { r1.draw(); r2.draw();}
Jun 9, 2014 IAT 265 7
Primitive typesg Primitive types are determined by
machine architecturebyte: 8bits reference: (JVM
Dependent)short: 16bitsint: 32bitslong: 64bitsfloat: 32bitsdouble: 64bits
Jun 9, 2014 IAT 265 8
Referenceg Like a remote controlg a reference is a primitive
thing that points at objects
g the new keyword causes the reference to point at a new instance of the object
Jun 9, 2014 IAT 265 9
Jun 9, 2014 IAT 265 10
Arraysg int[] nums = new int[7] ;
Jun 9, 2014 IAT 265 11
Array of objectsg Dog[] pets = new Dog[7];g It starts as an array of null
references
Jun 9, 2014 IAT 265 12
Array of objectsDog[] pets = new Dog[7] ;pets[0] = new Dog();pets[1] = new Dog();
IAT 265 13
Objects
Jun 9, 2014
IAT 265 14
Real Objectsg Real-world objects have
– State– Behavior
g Bicycle– State
• selected gear, current pedal cadence, speed– Behavior
• Change Gear, Set Cadence, Apply Brakes
Jun 9, 2014
IAT 265 15
Software Objectg State
int gear ;float speed ;float cadence ;
g BehaviorChangeGears(int g);Brake( float level );ChangeCadence( float c );int GetGear();float GetSpeed(); …
Jun 9, 2014
IAT 265 16
Java directly supports Objectsg Java has direct syntactic and semantic support for
ObjectsSyntax:
class Bicycle { private int cadence = 0; private int speed = 0; private int gear = 1; void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; }}
IAT 265 17
Java directly supports Objectsg Java has direct syntactic and semantic support for
ObjectsSemantics:
class Bicycle { private int cadence = 0;
private int speed = 0; private int gear = 1;
void changeCadence(int newValue) { cadence = newValue; } void changeGear(int newValue) { gear = newValue; }}
Only these methods can read or write Bicycle private data
IAT 265 18
Java Semantic supportg Programming usually takes place
with objects:ClockThing clock = new ClockThing();
clock.setSecond( 12 );clock.setMinute( 18 );clock.setHour( 3 );
Jun 9, 2014
IAT 265 19
Even Arrays are objects
int[] bob = new int[10] ;bob[4] = 123 ;println( bob.size() );
Bicycle[] bikes = new Bicycle[10] ;bikes[0] = new Bicycle();
Jun 9, 2014
IAT 265 20
Sets and Gets
g what can you do with private data?– to set it: setVarName( varType
newValue)– to get it: varType getVarName()
g Why?
Jun 9, 2014
IAT 265 21
Temperature objectclass temp // constructor not shown{ private float kelvin ; void setCelsius( float C ); { if( C < -273.15 )
return ; // perhaps an error message would be in order else kelvin = C + 273.15 ;
} float getCelsius() { return( kelvin - 273.15 ); } float setKelvin( float k ) { if( k < 0 )
return ; else kelvin = k ;
}}
IAT 265 22
Temperature objectg Controls accessg Ensures correctness
– can only run a setXYZ() to change temp– can only do getXYZ() to get the value in
the desired scaleg Who cares?
Jun 9, 2014
IAT 265 23
Who cares?g When you want to:
– Solve the problem once and forget it– Reuse the solution elsewhere– Establish rules for use and change of data
g The principle:– Information hiding– By interacting only with an object's methods,
the details of its internal implementation remain hidden from the outside world.
Jun 9, 2014
IAT 265 24
Principle: Code re-useg If an object already exists, you can
use that object in your program. g Specialists build, you use
Jun 9, 2014
IAT 265 25
Principle: Define the Interfaceg Define the interface:
– The list of methods with Defined Operation
g The interface is the thing that other people use
g If you have the same interface with the same meaning– You can plug in a better
implementation!Jun 9, 2014
IAT 265 26
Define the Interfaceg If you have the same interface with the
same meaning– You can plug in a better
implementation!– You can plug in a More Interesting
implementation!
Jun 9, 2014
IAT 265 27
Summary of principlesg Hide unnecessary detailsg Clearly define the interfaceg Allow and support code re-use
g Build on the work of others
Jun 9, 2014