Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An...

16
Objects

Transcript of Objects. The Heart of the Matter - The "Black Box" Object-oriented software is all about objects. An...

Page 1: 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.

Objects

Page 2: 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.

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.

Page 3: 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

• 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.

Page 4: 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.

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.

Page 5: 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.

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.

Page 6: 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.

“Apple” example from your text

methods

Variables(fields)

Page 7: 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.

A Representation Highlighting Encapsulation

Page 8: 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.

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

Page 9: 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.

A Spot Without a Classint x = 33;int y = 50;int diameter = 30;

void setup() { size(100, 100); smooth(); noStroke();}

void draw() { background(0); ellipse(x, y, diameter, diameter);}

Page 10: 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.

A Class Without MethodsSpot 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}

Page 11: 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.

A display() MethodSpot sp; // Declare the objectvoid 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); }}

Page 12: 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.

Adding a constructorSpot sp; // Declare the objectvoid 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); }}

Page 13: 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.

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

Page 14: 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.

The move() Behavior or Methodclass 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); }}

Page 15: 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.

Many SpotsSpot 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();}

Page 16: 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.

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.