1 / 16

Objects

Objects. The Heart of the Matter - The "Black Box". Object-oriented software is all about objects . An object is a "black box" which receives and sends messages (i.e., function calls).

eagan
Download Presentation

Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Objects

  2. The Heart of the Matter - The "Black Box" • Object-oriented software is all about objects. • An object is a "black box" which receives and sends messages (i.e., function calls). • A black box actually contains methods (functions) and data (variables) which the methods operate on. • In o-o (object-oriented) programming, methods and data are merged into a single indivisible thing -- an object.

  3. Messages • Why the "black box" metaphor for an object? • A primary rule of object-oriented programming is that: as the user of an object, you should never need to peek inside the box! • All communication to it is done via method calls. • So you shouldn't have to know anything about what is in the black box in order to use it. • Methods are functions.

  4. Encapsulation • As long as you deal with objects as black boxes via their methods (i.e., calling their functions), the software is guaranteed to work. • Providing access to an object only through its methods, while keeping the details of the code and the data (i.e., the variables) secret is called information hiding. • An equivalent term is encapsulation.

  5. Classes • How are objects defined? • An object is defined via its class, which determines everything about an object. • Objects are individual instances of a class. • For example, you may create an object call Spot from class Dog. The Dog class defines what it is to be a Dog object. • You can make more than one object of the Dog class, and call them Spot, Fido, Rover, etc.

  6. “Apple” example from your text Variables (fields) methods

  7. A Representation Highlighting Encapsulation

  8. Defining a Class: the Spot Class • Defining a class is like creating your own data type. • String, PImage and PFont are all data types defined by classes. • The Spot class will produce a white “dot” along with any needed behaviors. • What data do we need to define a “spot” • x, y position • diameter

  9. A Spot Without a Class int x = 33; int y = 50; int diameter = 30; void setup() { size(100, 100); smooth(); noStroke(); } void draw() { background(0); ellipse(x, y, diameter, diameter); }

  10. A Class Without Methods Spot sp; // Declare the object void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; // Assign 33 to the x field sp.y = 50; // Assign 50 to the y field sp.diameter = 30; // Assign 30 to the diameter field } void draw() { background(0); ellipse(sp.x, sp.y, sp.diameter, sp.diameter); } class Spot { // the Spot class float x, y; // The x- and y-coordinate float diameter; // Diameter of the circle }

  11. A display() Method Spot sp; // Declare the object void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(); // Construct the object sp.x = 33; sp.y = 50; sp.diameter = 30; } void draw() { background(0); sp.display(); // calling the display() method } class Spot { float x, y, diameter; void display() { // ç the display method ellipse(x, y, diameter, diameter); } }

  12. Adding a constructor Spot sp; // Declare the object void setup() { size(100, 100); smooth(); noStroke(); sp = new Spot(33, 50, 30); // Construct the object } void draw() { background(0); sp.display(); } class Spot { // Class definition float x, y, diameter; Spot(float xpos, float ypos, float dia) {// constructor method x = xpos; // Assign 33 to x y = ypos; // Assign 50 to y diameter = dia; // Assign 30 to diameter } void display() { ellipse(x, y, diameter, diameter); } }

  13. Encapsulation revisited • The Spot class was not really encapsulated until we add the constructor. • Without the constructor, the user of the class had to know how the data was stored to use the class

  14. The move() Behavior or Method class Spot { float x, y; // X-coordinate, y-coordinate float diameter; // Diameter of the circle float speed; // Distance moved each frame int direction = 1; // Motion Direction(1 is down, -1 is up) Spot(float xpos, float ypos, float dia, float sp) { // Constructor x = xpos; y = ypos; diameter = dia; speed = sp; } void move() { y += (speed * direction); if ((y > (height - diameter / 2)) || (y < diameter / 2)) { direction *= -1; } } void display() { ellipse(x, y, diameter, diameter); } }

  15. Many Spots Spot sp1, sp2, sp3; // Declare the objects void setup() { size(100, 100); smooth(); noStroke(); sp1 = new Spot(20, 50, 40, 0.5); // Construct sp1 sp2 = new Spot(50, 50, 10, 2.0); // Construct sp2 sp3 = new Spot(80, 50, 30, 1.5); // Construct sp3 } void draw() { fill(0, 15); rect(0, 0, width, height); fill(255); sp1.move(); sp2.move(); sp3.move(); sp1.display(); sp2.display(); sp3.display(); }

  16. Spot Arrays • “main” program • Spot class • Try making a Square class that is similar to the Spot class. • Create 2 Square objects and cause them to display.

More Related