Thanks for another fast response. Inheritance - I'm going to get this
-tonight-!
recap;
# the parent class
cat fx/ruby/chap3/class_Song.rb
#!/usr/bin/env ruby
class Song
def initialize(name, artist, duration)
# define instance variables
@name = name
@artist = artist
@duration = duration
end
def to_s
# method for displaying these instancess
"Song: #@name--#@artist (#@duration)"
end
end
# the child class
jayeola@tp20$ cat fx/ruby/chap3/class_KaraokeSong.rb
#!/usr/bin/env ruby
require 'class_Song.rb'
# test if this class has been loaded correctly
Object.const_defined?(:Song)
class KaraokeSong < Song
def initialize(name, artist, duration, lyrics)
super(name, artist, duration)
@lyrics = lyrics
end
def to_s
super + " [#@lyrics]"
end
end
#song = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...")
#song = KaraokeSong.new("Sinatra", 225, "And now, the...")
#song.to_s
#song.inspect
How do we get KaraokeSong to acquire it's parent's attributes -without-
hadrcoding "def initialize(name, artist, duration, lyrics)", unless one
has to? From the book I get the impression that you can get away with
specifying the child's stuff and that it will take properties from it's
parent with the keyword super....
--
John Maclean
MSc (DIC)
07739 171 531