Upgrade to Pro — share decks privately, control downloads, hide ads and more …

M250 Unit 2 Subsection 5 - Message answers and collaborating objects (9)

Matt
January 09, 2020
16

M250 Unit 2 Subsection 5 - Message answers and collaborating objects (9)

Matt

January 09, 2020
Tweet

More Decks by Matt

Transcript

  1. Recall that sending messages to an object sometimes results in

    a response back -- a message answer. A message answer is a returned value. It could be a number like 5, a character like ‘d’, or even a reference to an object.
  2. We can use message answers in different ways, and one

    way is to use them as arguments in other messages. Let’s see an example involving Frog objects.
  3. Let’s say we wanted to make a particular instance of

    Frog, kermit, the same colour as another instance of Frog, gribbit. Now what if we don’t know what colour gribbit is -- or better yet, we don’t need to know. We’re not interested whatsoever in the actual colour of gribbit or kermit. We just want to ensure they are the same colour.
  4. Among others, we can send Frog objects the following messages:

    • setColour() • getColour() We know we want to set kermit’s colour to gribbit’s, so kermit should be the receiver. So far we have: kermit.setColour()
  5. But what should the argument of kermit.setColour() be? It should

    be the colour of gribbit, which is returned as a message answer when we send gribbit the message getColour(). Imagine we have the colour of gribbit set to the variable x. Then you can see that the code we’d write to solve our problem would be: kermit.setColour(x) There’s no need to do this though. We can just use: kermit.setColour(gribbit.getColour())
  6. kermit.setColour(gribbit.getColour()) Remember that the dot in a message-send means to

    send the message to the right of it to the receiver to the left of it. 1] The message getColour() is sent to gribbit. 2] gribbit returns a message answer of its colour. 3] This message answer supplies the argument of setColour(). 4] kermit is sent the message setColour() with this argument. So kermit now knows which colour to set itself to, and does so.
  7. Frog objects actually understand the message sameColourAs(), which takes another

    Frog object as its argument. For example: kermit.sameColourAs(gribbit) Although this message looks a little simpler on the surface, the actual code that makes it work is very similar to the example we just looked at. That is, kermit.sameColourAs(gribbit) & kermit.setColour(gribbit.getPosition()) do the exact same things.
  8. These messages are an example of collaboration: two different objects

    have sent messages to each other in order to make something happen that we wanted to happen.
  9. Getter and setter messages Note how the message getColour() returns

    a message answer -- the colour of the receiver. Also note that setColour() doesn’t return a message answer. It certainly does something (changes the colour of the receiver) but it doesn’t return a value.
  10. In programming there are two very very common types of

    messages: - getter messages - setter messages
  11. Getter message These always return a message answer from the

    receiver. That’s their purpose. We use them to ‘get’ a value. Examples: getColour(), getPosition(), getHeight() Setter message Are used to change the state of the receiver. We use them to ‘set’ one or more attribute values of the receiver. We set these values to the argument(s) of the message, so setter messages always involve arguments. Examples: setColour(OUColour.GREEN), setPosition(2), setHeight(3)
  12. It is very common for the protocol of an object

    to include associated getter and setter message for each of its attributes. So if we have a Book object with the attributes: title, and author, then we would programme the object to understand the messages: • getTitle() - returns the value of title • setTitle() - sets the value of title • getAuthor() - returns the value of author • setAuthor() - sets the value of author
  13. Quick note about colour You might have noticed that we

    set the colour of a Frog object using an argument like OUColour.GREEN (for example) rather than simply the argument green. This will be explained and made clearer in later units. For now, just consider the two things one and the same: so OUColour.GREEN just means the colour green to us right now.
  14. A summary of what we know about classes so far

    • A class is like a blueprint for object creation. It defines a group of objects. • Instances of the same class have the same protocol, behaviour, and the same attributes as each other. • Each instance of a class is initialised in the same way -- they are created with the same state. • Each instance of a class has their own data – their own copy of their attributes that belong only to them and the value of which can change (for example by sending the object a setter message).