IAT 265 Objects ______________________________________________________________________________________ 1

advertisement
IAT 265
Objects
______________________________________________________________________________________
SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW.SIAT.SFU.CA
IAT 265
1
Outline

Object-oriented programming
– Object components
– Rocket
– Primitive types and Object References
 Objects,
another metaphor
 Why objects?
May 28, 2015
IAT 265
2
Classes vs Objects
A
Class is a blueprint for a bicycle
 An Object is a bicycle
 Many
May 28, 2015
bicycles, one blueprint
IAT 265
3
Parts of a class

Classes define fields, constructors and methods

Fields are the variables that will appear inside every
instance of the class
– Each instance has its own values


Constructors are special methods that define how to
build instances (generally, how to set the initial values
of fields)
Methods are how you do things to instances
May 28, 2015
IAT 265
4
Defining the rocket class
class Rocket
{
// fields
float rotation = 0;
float xPos;
float yPos;
final int halfWidth = 10;
final int halfHeight= 10;
void draw() {
pushMatrix();
translate(xPos, yPos);
rotate(rotation);
triangle(0, -halfHeight, -halfWidt
// constructor
Rocket( int initialX,
int initialY,
float initialRot )
{
xPos = initialX;
yPos = initialY;
rotation = initialRot;
}
rectMode(CORNERS);
rect(-halfWidth + 5, halfHeight, rect(halfWidth - 8, halfHeight, ha
popMatrix();
}
}
May 28, 2015
IAT 265
5
Using the class to create instances



Classes define a type
You can now declare variables of this type and initialize them using
the constructor
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();
}
May 28, 2015
IAT 265
6
Primitive types
 Primitive
types are determined by machine
architecture
byte:
short:
int:
long:
float:
double:
May 28, 2015
8bits
16bits
32bits
64bits
32bits
64bits
reference: (JVM Dependent)
IAT 265
7
Reference
 Like
a remote control
 a reference is a primitive
thing that points at objects
 the new keyword causes
the reference to point at
a new instance of the
object
May 28, 2015
IAT 265
8
May 28, 2015
IAT 265
9
Arrays
 int[]
May 28, 2015
nums = new int[7] ;
IAT 265
10
Array of objects
 Dog[]
pets = new Dog[7];
 It starts as an array of null references
May 28, 2015
IAT 265
11
Array of objects
Dog[] pets = new Dog[7] ;
pets[0] = new Dog();
pets[1] = new Dog();
May 28, 2015
IAT 265
12
Objects
May 28, 2015
IAT 265
13
Real Objects
 Real-world
objects have
– State
– Behavior
 Bicycle
– State
• selected gear, current pedal cadence, speed
– Behavior
• Change Gear, Set Cadence, Apply Brakes
May 28, 2015
IAT 265
14
Software Object

State
int gear ;
float speed ;
float cadence ;

Behavior
ChangeGears(int g);
Brake( float level );
ChangeCadence( float c );
int GetGear();
float GetSpeed();
…
May 28, 2015
IAT 265
15
Java directly supports Objects

Java has direct syntactic and semantic support for Objects
Syntax:
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
16
Java directly supports Objects

Java has direct syntactic and semantic support for Objects
Semantics:
class Bicycle
{
Only these methods
private int cadence = 0;
can read or write
Bicycle private data
private int speed = 0;
private int gear = 1;
void changeCadence(int newValue) {
cadence = newValue;
}
void changeGear(int newValue) {
gear = newValue;
}
}
IAT 265
17
Java Semantic support
 Programming
objects:
usually takes place with
ClockThing clock = new ClockThing();
clock.setSecond( 12 );
clock.setMinute( 18 );
clock.setHour( 3 );
May 28, 2015
IAT 265
18
Even Arrays are objects
int[] bob = new int[10] ;
bob[4] = 123 ;
println( bob.size() );
Bicycle[] bikes = new Bicycle[10] ;
bikes[0] = new Bicycle();
May 28, 2015
IAT 265
19
Sets and Gets
 what
can you do with private data?
– to set it:
– to get it:
setVarName( varType newValue)
varType getVarName()
 Why?
May 28, 2015
IAT 265
20
Temperature object
class 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
21
Temperature object
 Controls
access
 Ensures correctness
– can only run a setXYZ() to change temp
– can only do getXYZ() to get the value in the
desired scale
 Who
May 28, 2015
cares?
IAT 265
22
Who cares?

When you want to:
– Solve the problem once and forget it
– Reuse the solution elsewhere
– Establish rules for use and change of data

The principle:
– Information hiding
– By interacting only with an object's methods, the
details of its internal implementation remain hidden
from the outside world.
May 28, 2015
IAT 265
23
Principle: Code re-use
 If
an object already exists, you can use
that object in your program.
 Specialists build, you use
May 28, 2015
IAT 265
24
Principle: Define the Interface
 Define
the interface:
– The list of methods with Defined Operation
 The
interface is the thing that other people
use
 If you have the same interface with the
same meaning
– You can plug in a better implementation!
May 28, 2015
IAT 265
25
Define the Interface
 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!
May 28, 2015
IAT 265
26
Summary of principles
 Hide
unnecessary details
 Clearly define the interface
 Allow and support code re-use
 Build
May 28, 2015
on the work of others
IAT 265
27
Download