object oriented programming
play

Object Oriented Programming Objects and Classes Methods - PDF document

Outline Buggles and BuggleWorld Object Oriented Programming Objects and Classes Methods Contracts C - 1 C - 2 Unlearn what you have learned More Buggles argument betty double the Buggle protected class BuggleWorld


  1. Outline • Buggles and BuggleWorld Object Oriented Programming • Objects and Classes • Methods • Contracts C - 1 C - 2 Unlearn what you have learned More Buggles argument betty double the Buggle protected class BuggleWorld static object becky the Buggle variable float bernice the Buggle C - 3 C - 4 “I don’t think that word means what you think it means” Buggle properties The Java “Class” When we create new items in Java (such as a • Buggles have four properties... number, a picture, a file, or a Buggle-like – position: The location of the Buggle in BuggleWorld - creature), we first have to describe the item’s specified by an (X,Y) coordinate properties. – heading: The compass direction the Buggle is facing (NORTH, SOUTH, EAST, or WEST) – color: The color of the Buggle (and its painted trail) We do this in Java with a class – brushDown: Whether the Buggle’s brush is down (the Buggle is leaving a trail behind it) Each new item we create with these properties is called an object C - 5 C - 6

  2. Buggles as Objects What is a Class? • A class can be thought of as a template, or a mold, for A class is described by: creating an object • instance variables - Instance variables • Every object is a member of a class describe the properties of each class instance • Objects are also called instances - each object is a (each object of the class). specific instance of its class • We created 3 Buggles. Buggles are objects belonging to the Buggle class • Each of our 3 Buggle objects has the properties described by the Buggle class C - 7 C - 8 Buggle Objects More Buggles • In the Buggle class, every Buggle object has 4 properties (instance variables) betty the Buggle • So becky, betty, and bernice each have the same 4 properties, but they may have different states, i.e., the value of the properties may differ for each BuggleWorld of our Buggles becky the Buggle - becky: (1,1); EAST; red; true bernice - betty: (4,6); NORTH; blue; true the Buggle - bernice: (6,1); EAST; magenta; false C - 9 C - 10 Changing Instance Variables Go, becky, Go! • How do we change the state of an object becky.forward(); (such as a Buggle)? In other words, how do becky.forward(); we change the value of an instance variable? becky.left(); - We send the object a message • Messages you can send Buggles include betty.backward(); forward , backward , left , and right bernice.forward(); becky.forward(); bernice.forward(); becky.left(); C - 11 C - 12

  3. What is a Class? Instance Methods A class is described by: becky.forward(); becky.left(); • instance variables - Instance variables describe the properties of each class instance (each object The statements above send messages to becky. of the class). When an object receives a message, it executes a set of instructions called a method. • instance methods - Instance methods are the messages an instance of the class can respond to. The general form of invoking a method is object.method (); C - 13 C - 14 Methods with Arguments (aren’t we sassy?) Contracts • Some methods require additional information when they are invoked • Every class has a contract that specifies the behavior of its methods, i.e., how instances of the class respond • The additional information you provide is to messages called an argument • Any user of a class can expect that objects will behave Examples: as described in the contract becky.setColor(Color.yellow); • Any implementer of the class must ensure that objects fulfill the contract becky.forward(5); becky.backward(3); C - 15 C - 16 It’s deja vu all over again A Class as a Black Box Contract • Remember abstraction and interfaces ? • Recall: Once you learn how to drive, Implementer / you can drive any car - they all have User / Client Designer common features (a common interface) such as steering, gas, and breaking). Class Once you learn these, you can use any Black Box car. You Lyn C - 17 C - 18

  4. Let’s review... What is a Class? • becky is an object. What kind of object? A Buggle. A class is described by: She is a member of the Buggle class. • instance variables - Instance variables describe • She (as a Buggle) has certain properties (instance the properties of each class instance (each object variables): position , heading , color , and of the class). brushDown. • instance methods - Instance methods are the • To change her properties (instance variables), we send her a message (we invoke a method)... messages an instance of the class can respond to. becky.backward(7); • constructor - The constructor is a special method which helps create new instance of the • The set of messages we can send her is specified in the contract. class. C - 19 C - 20 Constructor Buggle Class Buggle Class • Every class has a special method (or methods) called a constructor position heading instance variables color • A constructor helps create new instances of brushDown the class constructor Buggle() • The constructor is invoked by using the new forward() backward() operator and the name of the class left() right() instance methods Buggle becky = new Buggle(); forward(int n) backward(int n) setColor(Color c) ... C - 21 C - 22 FirstBuggleExample import java.awt.*; public class FirstBuggleExample extends BuggleWorld { public void init() { super.init(); setDimensions(9,9); } public void run () { // Create new Buggle and move her around Buggle becky = new Buggle(); becky.forward(); becky.forward(); becky.left(); becky.forward(); } C - 23 }

Recommend


More recommend