Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mapping Methods With Arguments

Similar presentations


Presentation on theme: "Mapping Methods With Arguments"— Presentation transcript:

1 Mapping Methods With Arguments
This slidedeck shows how the areas of our “under the hood” map get modified when you call a method that takes another object as input. We again use the Dillo class, but this time after we have written a longerThan method (that takes another Dillo as input).

2 We start with two objects bound to the names babyDillo and adultDillo.
KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } We start with two objects bound to the names babyDillo and adultDillo. Notice that each object contains a copy of the method NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo

3 The name this now refers to adultDillo, and other refers to babyDillo
KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } Now we call the longerThan method on adultDillo, passing babyDillo as the argument The name this now refers to adultDillo, and other refers to babyDillo NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo adultDillo.longerThan(babyDillo) this other

4 EXPRESSION (impact on other areas are in red)
KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } The expression being evaluated now changes to the body of the method from the adultDillo class NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo this.length > other.length this other

5 Substitute these values in the expression
KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } To evaluate this expression, follow the arrows from the names: this refers to the lower object, which has length 24, while other refers to the upper object Substitute these values in the expression NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo 24 > 8 this other

6 EXPRESSION (impact on other areas are in red)
KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } Next we execute the return, which ends the method. The this and other names go away (they are only active while the method is being evaluated) NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo return true

7 EXPRESSION (impact on other areas are in red)
KNOWN CLASSES OBJECTS class Dillo { int length; boolean isDead; Dillo (int length, boolean isDead) { this.length = length; this.isDead = isDead; } boolean longerThan(Dillo other) { return (this.length > other.length); Dillo: length = 8; isDead = false; boolean longerThan(Dillo other) { return (this.length > other.length); } Dillo: length = 24; isDead = true boolean longerThan(Dillo other) { return (this.length > other.length); } This example illustrates how names (this and parameters) are added to the map when calling a method, then removed when a method is finished NAMED VALUES EXPRESSION (impact on other areas are in red) babyDillo adultDillo


Download ppt "Mapping Methods With Arguments"

Similar presentations


Ads by Google