给定以下课程
class Parent
def hello
puts "I am the parent class"
end
def call_parent_hello
hello
end
end
class Child < Parent
def hello
puts "I am the child class"
end
end
当我执行以下操作时:
c = Child.new
c.hello # => Outputs: "I am the child class"
c.call_parent_hello # => Outputs: "I am the child class"
是否可以Child#call_parent_hello
访问Parent#hello
,但不改变Parent
类?
我正在寻找这样的某种called_by_parent_class?
实现:
def hello
if called_by_parent_class?
super
else
puts "I am the child class"
end
end