260 likes | 387 Vues
This lecture covers the foundational concepts of accessor and mutator methods in Java, which are essential for managing object properties. Accessor methods, typically starting with "get," return object property values, while mutator methods, starting with "set," allow modification of those values. Through examples, such as the EcoSystem and Terrarium classes, we demonstrate how these methods work in practice. Key concepts include method calls, parameter handling, and the differences between different types of relationships within object-oriented programming.
E N D
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall 645-4739 alphonce@buffalo.edu
Phones off Signs out
Agenda • non-void method • accessor method • method with parameters • mutator method • method calls in detail • association relationship
Accessor method • A method which returns information, the value of an object’s property. • These methods typically have names that start with “get”.
Accessor example • simple example of a method that returns a value public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public example1.Terrarium getTerrarium() { return _t; } }
Mutator methods • A method which accepts information and sets the value of an object’s property. • So-called because method changes (mutates) the value of a property. • Mutator methods typically have names that start with “set”.
Mutator example public class EcoSystem { private example1.Terrarium _t; public EcoSystem() { _t = new example1.Terrarium(); } public void setTerrarium(example1.Terrarium t){ _t = t; } }
EcoSystemes = new EcoSystem(); Blackboard example EcoSystem es Terrarium _t
EcoSystemes = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); Blackboard example EcoSystem es Terrarium _t Terrarium terra
EcoSystemes = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); Blackboard example EcoSystem es Terrarium _t Terrarium terra
EcoSystemes = new EcoSystem(); es 1025 Blackboard example runtimestack invocation record 875 950 Terrarium heap 1025 950 _t EcoSystem EcoSystem
EcoSystemes = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es 1025 Blackboard example terra 875 runtimestack 875 Terrarium 950 Terrarium heap 1025 950 _t EcoSystem EcoSystem
EcoSystemes = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); es 1025 Blackboard example terra 875 t 875 runtimestack invocation record 875 Terrarium 950 Terrarium heap 1025 950 _t EcoSystem EcoSystem
EcoSystemes = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); es 1025 Blackboard example terra 875 t 875 runtimestack Control transfers to called method. Argument’s value is assigned to parameter. After method body is executed, control returns to statement after method call. _t = t; 875 Terrarium _t = t assignment copies contents of parameter to the instance variable 950 Terrarium heap 1025 875 _t EcoSystem EcoSystem
EcoSystemes = new EcoSystem(); example1.Terrarium terra = new example1.Terrarium(); es.setTerrarium(terra); es 1025 Blackboard example terra 875 runtimestack After method call, invocation record is removed from runtime stack. 875 Terrarium 950 Terrarium heap 1025 875 _t EcoSystem EcoSystem
java.awt.Color class • Instances of the java.awt.Color class represent colors. • A color is described by how much of each of the three colors red, green and blue are mixed together. • There are some predefined color objects in the java.awt.Color class, such as: java.awt.Color.RED java.awt.Color.BLUE
getColor • Both the Ant and the Caterpillar have getColor methods. • By calling their getColor methods we can find out the color of our Ant and Caterpillar objects. • The getColor method requires no argument in the method call.
Example Suppose that the variable a refers to an Ant. Then the following method call gets a reference to the color of the Ant object: a.getColor() We can assign that reference to a variable: java.awt.Colorcol = a.getColor();
setColor • Both the Ant and the Caterpillar have setColor methods. • By calling their setColor methods we can change the color of our Ant and Caterpillar objects. • The setColor method requires that a reference to a java.awt.Color object be provided as an argument in the method call.
Example Suppose again that the variable a refers to an Ant, and suppose further that thevariable c refers to a Caterpillar. Then the following method call sets the color of the Ant object to red: a.setColor(java.awt.Color.RED) Likewise, the following method call sets the color of the Caterpillar object to blue: c.setColor(java.awt.Color.BLUE)
Making c and a have the same color c.setColor(a.getColor()) • Swapping colors with two additional variables: java.awt.ColorcolorOne = a.getColor(); java.awt.ColorcolorTwo = c.getColor(); c.setColor(colorOne); a.setColor(colorTwo); • Exercise: can you swap the colors with just one additional variable.
Another relationship • One collar for its life…? • A dog has-a tail; the tail is a part of the dog. • We need something different to model a relationship like dog and collar.
Association • Also called “knows a”. • A relationship of knowing (e.g. Dog-Collar as opposed to Dog-Tail) – (back to Clifford!) • No necessary lifetime link • We’ll look at two different implementations of “knows a”: • The first we will see today, and is very similar to our implementation of “has a”. • The second, which we will see next time, is a bit more complex but is also more flexible.
First implementation In Java code, the first involves 3 changes to the “knowing” class: • Declaration of instance variable of the “known” class/type (because the “knowing” object will want to communicate with the “known” object). • Assignment of existing “known” instance to the instance variable (because the instance variable must refer to an object). • Parameter of “known” class in “knowing” class constructor (because the creator of an instance of the “knowing” class needs to supply an instance of the “known” class).
Dog – Collar example in Java public class Dog { private Collar _collar; public Dog(Collar collar) { _collar = collar; } }