objects. 2 object oriented programming (oop) oop is a different way to view programming models the...

20
Objects

Upload: monica-austell

Post on 31-Mar-2015

237 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Objects

Page 2: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

2

Object Oriented Programming (OOP)OOP is a different way to view programming

Models the real world

A different view than ‘Structured’ programmingFunctions break a program into ‘modules’ like an outline

What is an Object?Usually based on a ‘noun’

It has ‘properties’

It has ‘actions’ (methods) that it can do (verbs)

An examples of an object that you know:String

The dot operator is used to make the String do work!

Page 3: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Why Objects?

OOP allows you to put data about something and actions it can do together into one “object”

OOP has been around since the 1960s

Most popular current languages have either adapted or were designed with OOP

C C++ C#

JavaScript

Java

Page 4: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

4

Object Example: Human

AttributesAgeHeightWeightNameEye colorHair color…

Functions SleepWake-UpEatRide something

Page 5: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

5

Classes and ObjectsA Class

A ‘plan’ for an object: Cookie CutterThe general idea of a thingHas placeholders for details, but not ‘made’ yetIt declares functions that could be done to an object

An ObjectAn ‘instance’ of a Class: A CookieA ‘real’ thing that has all of the specificsIt can be ‘told’ to execute it’s functions

You have to have a ‘plan’ before you can make an object:

Class before Object

Page 6: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Exercise 8.1: Plan a Car Class

Page 7: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Setup for a ‘Car’ before Objects

7

Use global variables for ‘properties’Color: carColorLocation: carX, carYSpeed: carSpeed

In setup()Set the carColorSet the starting locationSet the initial speed

In draw()Fill backgroundDisplay car at location with color (may use a function)Increment car’s location based on speed

Page 8: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Setup a ‘Car’ using Objects

8

One big difference with objects is that you move all of the global variables inside the Car object

Color: carColorLocation: carX, carYSpeed: carSpeedCar object instead!

We end up with one variable to represent the car

Instead of initializing all of those variables, we initialize the car object!

Page 9: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Setup a ‘Car’ using Objects

9

Outside of all methods (global)Declare a ‘parking place’ for a car

In setup()Make a new car object

based on the ‘Car’ plan)

Sets initial values for color, location and speed

In draw()Fill background

Tell the car to ‘move’ based on speed

Tell the car to ‘display’ itself (call a function)

Page 10: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

The ‘Car’ classConvert the non-OOP Car Data to a ClassNon-OOP OOP

Page 11: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Setup a ‘Car’ using Objects - Functions

11

Move the functions that did things to the carMove the code to inside the Car class

The will be called methods of the class

Move the code that was in display()and drive()

Page 12: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

The whole ‘Car’ (Example 8.1)class Car { // Define a class for a car color c; // Variables. float xpos; float ypos; float xspeed;

Car() { // A constructor. c = color(175); xpos = width/2; ypos = height/2; xspeed = 1; }

void display() { // Function. // The car is just a square rectMode(CENTER); stroke(0); fill(c); rect(xpos,ypos,20,10); }

void move() { // Function. xpos = xpos + xspeed; if (xpos > width) { xpos = 0; } }}

Page 13: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

What was that about a ‘Constructor’?A constructor is a special method of a class

Has the same name as the class

“Builds’ the objectSets initial values

It is ‘called’ with you use new:

class Car {

Car() { // Constructor c = color(175); xpos = width/2; ypos = height/2; xspeed = 1; }

void setup() { size(200,200); // Initialize Car object myCar = new Car();}

Page 14: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

And here is the OOPized versionIs the OOP version shorter?

Is it easier to read?Not yet maybe, but soon, and for the rest of your life.

14

Page 15: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Car Class should really be in its own file…

Click ‘Right Arrow’ in tabs rowUse ‘New Tab’ in menu to create a new fileName the new tab exactly like the class = ‘Car’Move the code for the ‘Car’ class to itSaves to a ‘Car.pde’ file in your folder

Keep both files in the same folder

If you want to use your ‘Car’ class in another project, simply copy the ‘Car.pde’ file there

Page 16: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Your first multi-file Project!

Page 17: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

What if we want to make more cars?

Right now, all cars are exactly the sameTheir constructor sets up the color, location…

How could we make ‘custom’ cars?Remember parameters?What if a Car constructor took parameters?class Car { Car(color colp, int xp, int yp, int speedp) { c = colp; xpos = xp; ypos = yp; xspeed = speedp; }

Page 18: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Now we can make two ‘custom’ carsUse your new ‘parameterized’ constructor!

Page 19: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

And imagine an array of Cars!You can use the ‘Car’ class just like any other type

Declare an array of our new Cars object:Car [ ] parkingLot;

setup() {

parkingLot = new Car[10];

}

But wait… what just happened?Did you create 10 cars? No, not yet.

You created 10 parking stalls for cars

So we still have to ‘build’ the cars and set all of the colors, locations and speeds…

parkingLot[0] = new Car(color, x, y..);

Page 20: Objects. 2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming

Filling the parking lot the easy way!Once you have the parking lot created,

Car [ ] parkingLot;

setup() {

parkingLot = new Car[10];

}

Use a for loop to make a bunch of cars! for (int i; i < 10; i++) {

parkingLot[i] = new Car(color, x, y..);

}

Arrays and loops work wonderfully together!

And they are even more fun with objects!