IAT 265 Objects SCHOOL OF INTERACTIVE ARTS TECHNOLOGY
IAT 265 Objects ___________________________________________ SCHOOL OF INTERACTIVE ARTS + TECHNOLOGY [SIAT] | WWW. SIAT. SFU. CA IAT 265 1
Outline g Object-oriented programming – Object components – Rocket – Primitive types and Object References g Objects, another metaphor g Why objects? May 26, 2015 IAT 265 2
Classes vs Objects g. A Class is a blueprint for a bicycle g An Object is a bicycle g Many May 26, 2015 bicycles, one blueprint IAT 265 3
Parts of a class g 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 May 26, 2015 IAT 265 4
Defining the rocket class Rocket { // fields float rotation = 0; float x. Pos; float y. Pos; final int half. Width = 10; final int half. Height= 10; void draw() { push. Matrix(); translate(x. Pos, y. Pos); rotate(rotation); triangle(0, -half. Height, -half. Widt // constructor Rocket( int initial. X, int initial. Y, float initial. Rot ) { x. Pos = initial. X; y. Pos = initial. Y; rotation = initial. Rot; } rect. Mode(CORNERS); rect(-half. Width + 5, half. Height, rect(half. Width - 8, half. Height, ha pop. Matrix(); } } May 26, 2015 IAT 265 5
Using the class to create instances g g g 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 r 1, r 2 ; void setup() { r 1 = new Rocket(75, 10, 0); r 2 = new Rocket(50, PI/2); } void draw() { r 1. draw(); r 2. draw(); } May 26, 2015 IAT 265 6
Primitive types g Primitive types are determined by machine architecture byte: short: int: long: float: double: May 26, 2015 8 bits 16 bits 32 bits 64 bits reference: (JVM Dependent) IAT 265 7
Reference g Like a remote control g 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 May 26, 2015 IAT 265 8
May 26, 2015 IAT 265 9
Arrays g int[] May 26, 2015 nums = new int[7] ; IAT 265 10
Array of objects g Dog[] pets = new Dog[7]; g It starts as an array of null references May 26, 2015 IAT 265 11
Array of objects Dog[] pets = new Dog[7] ; pets[0] = new Dog(); pets[1] = new Dog(); May 26, 2015 IAT 265 12
Objects May 26, 2015 IAT 265 13
Real Objects g Real-world objects have – State – Behavior g Bicycle – State • selected gear, current pedal cadence, speed – Behavior • Change Gear, Set Cadence, Apply Brakes May 26, 2015 IAT 265 14
Software Object g State int gear ; float speed ; float cadence ; g Behavior Change. Gears(int g); Brake( float level ); Change. Cadence( float c ); int Get. Gear(); float Get. Speed(); … May 26, 2015 IAT 265 15
Java directly supports Objects g 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 change. Cadence(int new. Value) { cadence = new. Value; } void change. Gear(int new. Value) { gear = new. Value; } } IAT 265 16
Java directly supports Objects g 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 change. Cadence(int new. Value) { cadence = new. Value; } void change. Gear(int new. Value) { gear = new. Value; } } IAT 265 17
Java Semantic support g Programming objects: usually takes place with Clock. Thing clock = new Clock. Thing(); clock. set. Second( 12 ); clock. set. Minute( 18 ); clock. set. Hour( 3 ); May 26, 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 26, 2015 IAT 265 19
Sets and Gets g what can you do with private data? – to set it: – to get it: set. Var. Name( var. Type new. Value) var. Type get. Var. Name() g Why? May 26, 2015 IAT 265 20
Temperature object class temp // constructor not shown { private float kelvin ; void set. Celsius( float C ); { if( C < -273. 15 ) return ; // perhaps an error message would be in order else kelvin = C + 273. 15 ; } float get. Celsius() { return( kelvin - 273. 15 ); } float set. Kelvin( float k ) { if( k < 0 ) return ; else kelvin = k ; } } IAT 265 21
Temperature object g Controls access g Ensures correctness – can only run a set. XYZ() to change temp – can only do get. XYZ() to get the value in the desired scale g Who May 26, 2015 cares? IAT 265 22
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. May 26, 2015 IAT 265 23
Principle: Code re-use g If an object already exists, you can use that object in your program. g Specialists build, you use May 26, 2015 IAT 265 24
Principle: Define the Interface g 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! May 26, 2015 IAT 265 25
Define the Interface g 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 26, 2015 IAT 265 26
Summary of principles g Hide unnecessary details g Clearly define the interface g Allow and support code re-use g Build May 26, 2015 on the work of others IAT 265 27
- Slides: 27