我想限制子类中超类方法的访问
class Parent
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
@first_name, @last_name = first_name, last_name
end
def full_name
@first_name + " " + @last_name
end
end
class Son < Parent
attr_accessor :first_name
def initialize(parent, first_name)
@first_name = first_name
@last_name = parent.last_name
end
def full_name
@first_name + " " + @last_name
end
end
p = Parent.new("Bharat", "Chipli")
puts p.full_name
s = Son.new(p, "Harry")
s.last_name= "Smith"
puts s.full_name
在这里,我将儿子的全名命名为“Harry Smith”,但我想要“Harry Chipli”