500 likes | 1.06k Views
Classes / Objects. An introduction to object-oriented programming. Object-oriented programming (OOP) allows you, the programmer, to create objects You have been using objects all along, you just weren’t aware of it …
E N D
Classes / Objects An introduction to object-oriented programming
Object-oriented programming (OOP) allows you, the programmer, to create objects • You have been using objects all along, you just weren’t aware of it … • Every time you call a rect(); or an ellipse();, you are calling an object called an ellipse. The ellipse has properties (x,y,w,h) What is object-oriented programming?
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 … and Processing Why Objects?
Attributes Functions Sleep Wake-Up Eat Ride something Human example • Age • Height • Weight • Name • Eye color • Hair color • …
A Class An Object An ‘instance’ of a Class: A Cookie A ‘real’ thing that has all of the specifics It can be ‘told’ to execute it’s functions Classes and Objects • A ‘plan’ for an object: Cookie Cutter • The general idea of a thing • Has placeholders for details, but not ‘made’ yet • It declares functions that could be done to an object • You have to have a ‘plan’ before you can make an object: • Class before Object
Let’s plan a simple Car • List data and functions Plan a Car Class • Let’s compare how we could convert the function we wrote to show different cars with objects • We’ll use our setup() and draw() methods
Use global variables for ‘properties’ • Color: carColor • Location: carX, carY • Speed: carSpeed • In setup() • Set the carColor • Set the starting location • Set the initial speed • In draw() • Fill background • Display car at location with color (may use a function) • Increment car’s location based on speed Setup for a ‘Car’ before Objects
Setup multiple ‘Cars’ before Objects • Use global arrays of variables for ‘properties’ • Allows us to use a loop to initialize a number of Cars • Declare Color array: carColor [ ] • Declare carX array: carX [ ] • Declare carY array: carY [ ] • Declare carSpeed array: carSpeed [ ] • In setup() • Set and initialize the arrays • In draw() • Fill background • Loop through all the cars • Display car at location with color (may use a function) • Increment car’s location based on speed
Setup a ‘Car’ using Objects We end up with one variable to represent the car Instead of initializing all of those variables, we initialize the car object! • One big difference with objects is that you move all of the global variables inside the Car object • Color: carColor • Location: carX, carY • Speed: carSpeed • Car object instead!
Setup a ‘Car’ using Objects • 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)
The ‘Car’ class Convert the non-OOP Car Data to a Class Non-OOP OOP
Setup a ‘Car’ using Objects - Data • Let’s break it down step by step • Declare the plan for the ‘Car’ • Outside draw() and setup() • Put variables for color, location and speed inside
Setup a ‘Car’ using Objects - Constructor • We need to write instructions to build a car • It is called the ‘Constructor’ method • Move the code that was in setup() • Set variables for color, location and speed inside
Setup a ‘Car’ using Objects - Functions • Move the functions that did things to the car • Move the code to inside the Car class • The will be called methods of the class • Move the code that was in display()and drive()
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 drive() { // Function. xpos = xpos + xspeed; if (xpos > width) { xpos = 0; } } }
What was that about a ‘Constructor’? A constructor is a special method of a class Has the same name as the class “Builds’ the object Sets initial values It is ‘called’ with you use new: void setup() { size(200,200); // Initialize Car object myCar = new Car(); } class Car { Car() { // Constructor c = color(175); xpos = width/2; ypos = height/2; xspeed = 1; }
Is the OOP version shorter? • Is it easier to read? • Not yet maybe, but soon, and for the rest of your life. And here is the OOPized version
What if we want to make more cars? Right now, all cars are exactly the same Their 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, intxp, intyp, intspeedp) { c = colp; xpos = xp; ypos = yp; xspeed = speedp; }
Please review parameter passing… Make a new frog with a length of 100
Now we can make two ‘custom’ cars Use your new ‘parameterized’ constructor!
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..);
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 (inti; i < 10; i++) { parkingLot[i] = new Car(color, x, y..); } Arrays and loops work wonderfully together! And they are even more fun with objects!
Objects provide a different way of designing programs • Combine data and functionality • A class is a ‘plan’ for an object • An object is an ‘instance’ of a class • The ‘new’keyword is used to instantiate (create) an object from a class • Use the ‘dot’ operator ‘.’ to access member data and functions of the class • Classes are normally written in separate files • One class per file • Put the class ‘source’ file in the same folder as the ‘main’ Summary