The game in 2adventure.rb will have two classes descending from Thing: the Treasure class and the Room class. The Treasure class adds a value
attribute, which can be both read and written. Note that its initialize
method calls its superclass in order to initialize the name
and description
attributes before initializing the new @value
variable:
super( aName, aDescription ) @value = aValue
Here, if I had omitted the call to the superclass, the name
and description
attributes would never be initialized. This is because Treasure.initialize
overrides Thing.initialize
, so when a Treasure object is created, the code in Thing.initialize
will not automatically be executed.
On the other hand, the Room class, which also descends from Thing, currently has no initialize
method, so when a new Room object is created, Ruby goes scrambling back up the class hierarchy in search of one. The first initialize
method it finds is in Thing, so a Room object’s name
and description
attributes are initialized there.
18.117.77.158