Download presentation
Presentation is loading. Please wait.
Published byMonica Austell Modified over 10 years ago
1
Objects
2
2 Object Oriented Programming (OOP) OOP is a different way to view programming Models the real world A different view than Structured programming Functions 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!
3
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
4
4 Object Example: Human Attributes Age Height Weight Name Eye color Hair color … Functions Sleep Wake-Up Eat Ride something
5
5 Classes and Objects A Class 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 An Object An instance of a Class: A Cookie A real thing that has all of the specifics It can be told to execute its functions You have to have a plan before you can make an object: C lass before O bject
6
Exercise 8.1: Plan a Car Class Lets plan a simple Car List data and functions Lets compare how we could convert the function we wrote to show different card with objects Well use our setup() and draw() methods
7
Setup for a Car before Objects 7 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 cars location based on speed
8
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: carColor Location: carX, carY Speed: carSpeed Car object instead! We end up with one variable to represent the car Instead of initializing all of those variables, we initialize the car object!
9
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)
10
The Car class Convert the non-OOP Car Data to a Class Non-OOPOOP
11
Setup a Car using Objects - Functions 11 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()
12
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; }
13
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 : 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(); }
14
And here is the OOPized version Is the OOP version shorter? Is it easier to read? Not yet maybe, but soon, and for the rest of your life. 14
15
Car Class should really be in its own file… Click Right Arrow in tabs row Use New Tab in menu to create a new file Name the new tab exactly like the class = Car Move the code for the Car class to it Saves 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
16
Your first multi-file Project!
17
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, int xp, int yp, int speedp) { c = colp; xpos = xp; ypos = yp; xspeed = speedp; }
18
Now we can make two custom cars Use your new parameterized constructor!
19
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..);
20
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!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.