クラスPersonを継承し、instrument属性を追加するMusicianというクラスを作成しようとしています。ミュージシャンのクラスが間違っていることは知っていますが、Rubyの正しい形式を知りたかっただけです。これが私のコードのすべてです:
class Person
attr_reader :first_name, :last_name, :age
def initialize (first_name, last_name, age)
@first_name = first_name
@last_name = last_name
@age = age
end
end
p = Person.new("Earl", "Rubens-Watts", 2)
p.first_name
p.last_name
p.age
class Musician < Person
attr_reader :instrument
def initialize (instrument)
@instrument = instrument
end
end
m = Musician.new("George", "Harrison", 58, "guitar")
m.first_name + " " + m.last_name + ": " + m.age.to_s
m.instrument
助けてくれてありがとう!