IAT 265 Objects
270 likes | 403 Vues
This guide delves into the fundamentals of object-oriented programming (OOP), explaining the difference between classes and objects. It illustrates how classes serve as blueprints for creating objects, using the example of a Rocket class. Key concepts such as fields, constructors, and methods are defined. The importance of objects in managing state and behavior in real-world applications is emphasized, along with the relevance of primitive types and references. This resource is ideal for beginners looking to grasp OOP principles and their application in Java.
IAT 265 Objects
E N D
Presentation Transcript
IAT 265 Objects IAT 265
Outline • Object-oriented programming • Object components • Rocket • Primitive types and Object References • Objects, another metaphor • Why objects? IAT 265
Classes vs Objects • A Class is a blueprint for a bicycle • An Object is a bicycle • Many bicycles, one blueprint IAT 265
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 IAT 265
Defining the rocket class class Rocket { // fields float rotation = 0; float xPos; float yPos; final inthalfWidth = 10; final inthalfHeight= 10; // constructor Rocket( intinitialX, intinitialY, 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(); } } IAT 265
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(); } IAT 265
Primitive types • Primitive types are determined by machine architecture byte: 8bits reference: (JVM Dependent) short: 16bits int: 32bits long: 64bits float: 32bits double: 64bits IAT 265
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 IAT 265
Arrays • int[] nums = new int[7] ; IAT 265
Array of objects • Dog[] pets = new Dog[7]; • It starts as an array of null references IAT 265
Array of objects Dog[] pets = new Dog[7] ; pets[0] = new Dog(); pets[1] = new Dog(); IAT 265
Objects IAT 265
Real Objects • Real-world objects have • State • Behavior • Bicycle • State • selected gear, current pedal cadence, speed • Behavior • Change Gear, Set Cadence, Apply Brakes IAT 265
Software Object • State int gear ; float speed ; float cadence ; • Behavior ChangeGears(int g); Brake( float level ); ChangeCadence( float c ); int GetGear(); float GetSpeed(); … IAT 265
Java directly supports Objects • Java has direct syntactic and semantic support for Objects Syntax: class Bicycle { privateint cadence = 0; privateint speed = 0; privateint gear = 1; void changeCadence(intnewValue) { cadence = newValue; } void changeGear(intnewValue) { gear = newValue; } } IAT 265
Java directly supports Objects • Java has direct syntactic and semantic support for Objects Semantics: class Bicycle { privateint cadence = 0; privateint speed = 0; privateint gear = 1; void changeCadence(intnewValue) { cadence = newValue; } void changeGear(intnewValue) { gear = newValue; } } Only these methods can read or write Bicycle private data IAT 265
Java Semantic support • Programming usually takes place with objects: ClockThingclock = new ClockThing(); clock.setSecond( 12 ); clock.setMinute( 18 ); clock.setHour( 3 ); IAT 265
Even Arrays are objects int[] bob = new int[10] ; bob[4] = 123 ; println( bob.size() ); Bicycle[] bikes = new Bicycle[10] ; bikes[0] = new Bicycle(); IAT 265
Sets and Gets • what can you do with private data? • to set it: setVarName( varType newValue) • to get it: varType getVarName() • Why? IAT 265
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
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 cares? IAT 265
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. IAT 265
Principle: Code re-use • If an object already exists, you can use that object in your program. • Specialists build, you use IAT 265
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! IAT 265
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! IAT 265
Summary of principles • Hide unnecessary details • Clearly define the interface • Allow and support code re-use • Build on the work of others IAT 265