210 likes | 310 Vues
Understand the fundamentals of Object-Oriented Programming (OOP) including objects, classes, messages, and methods. Learn how objects have independent attributes and methods, classes define templates for objects, and how objects communicate through messages. Explore examples and comparisons between objects and classes.
E N D
Objects • Object attribute definition allows for object to have independent attribute values. Ex: Benjamin & Bernie are customers of HomeCare share some similar information like a name, an address, & a budget. • Beside attribute, object also have some method. Ex: Benjamin & Bernie as customers have some behaviour like making purchase.
Object Structure Benjamin as an object: attributes: name=“Benjamin” address=“1, Robinson road” budget=“2000” methods: purchase() {send a purchase request} getBudget() {return budget}
Object Structure Bernie as an object: attributes: name=“Bernie” address=“18, Sophia road” budget=“1000” methods: purchase() {send a purchase request} getBudget() {return budget}
Class • A Class is a definition template for structuring and creating objects with the same attributes and methods. • Ex: Like Benjamin & Bernie, All customers of HomeCare share the same set of attribute & method definition. They all have attributes: name, address & budget, and methods purchase() & getBudget().
Class Class Customer: attributes: name address budget methods: purchase() {send a purchase request} getBudget() {return budget}
Object vs class • One major difference between object & class is in the way are treated in objects & class. • A Class attributes & methods are declarations that do not contain values. • An Object are created instances of a class that has it own attributes & methods (state).
Object & Class • Examine :Object & Class at salesperson
Object Structure Sean as an object: attributes: name=“Sean” methods: takeOrder() { check with the warehouse on stock abailability check with the warehouse on delivery schedule if ok then{instruct warehouse to deliver stock(address, date) return ok else return not ok}
Class Class Salesperson: attributes: name methods: takeOrder() { check with the warehouse on stock abailability check with the warehouse on delivery schedule if ok then{instruct warehouse to deliver stock(address, date) return ok else return not ok}
Message & Method • Objects communicate with one another by sending messages. • A messages is a method call from a message-sending object (sender) to a message-receiving object (receiver). • An object responds to message by executing one of its methods. • Such parameter allows for added flexibility in message passing. • Offcourse, an object may have as many methods as required.
Message Components • A message composed of: • An object identifier. • A method name. • arguments (parameter). • Ex: Benjamin sent a messages to Sean when Benjamin wanted to buy a sofa set. The reasonable location is in Benjamin’s purchase() method
Message components Benjamin as an object: attributes: name=“Benjamin” address=“1, Robinson road” budget=“2000” methods: purchase( Sean.takeOrder(“Benjamin”, “Sofa”,”1, Robinson Road”, “12 September 2012”) getBudget() {return budget} Sean is receiver of the message takeOrder is method call on Sean Reds are arguments of message
Method • A message is valid if the receiver has a method that corresponds to the method named in message & the appropriate arguments. • Ex: the takeOrder() message is valid because Sean has a corresponing method & the required arguments.
Object Structure Sean as an object: attributes: name=“Sean” methods: takeOrder(who,stock,address,date) { check with the warehouse on stock abailability check with the warehouse on delivery schedule if ok then{instruct warehouse to deliver stock to address on date return ok else return not ok}
Creating Objects • Objects are created from classess. • Created object instances are individuals with their own state. • Ex: Counter
First Object First Counter Object Attributes: number = 10 Methods: add() {number = number + 1} initialize() {number = 0} getNumber() {return number}
Second Object SecondCounter Object Attributes: number = 2 Methods: add() {number = number + 1} initialize() {number = 0} getNumber() {return number}
Third Objek ThirdCounter Object Attributes: number = 7 Methods: add() {number = number + 1} initialize() {number = 0} getNumber() {return number}
Class Counter Class Counter Attributes: number Methods: add() {number = number + 1} initialize() {number = 0} getNumber() {return number}
Summary • Objects are defined by classes. • Objects from the same class share the same definition of attributes and methods. • Objects from the same class may not have the same attribute values. • Objects from different classes do not share the same definition of attributes or methods. • Objects created from the same class share the same definition of attributes and methods but their state may differ. • A method is a set of operations executed by an object upon the receipt of a message. • A message has three components: an object identifier, a method name and arguments. • A message-receiving object is a server(receiver)to a message-sending object known as a client(sender).