class A
def a
puts 'in #a'
end
end
class B < A
def a
b()
end
def b
# here i want to call A#a.
end
end
发布于 2018-03-28 16:31:08
class B < A
alias :super_a :a
def a
b()
end
def b
super_a()
end
end
发布于 2018-03-28 17:45:59
没有很好的方法去做,但你可以A.instance_method(:a).bind(self).call
它会起作用的,但是很难看。
甚至可以在对象中定义自己的方法,以便在java中充当超级方法:
class SuperProxy
def initialize(obj)
@obj = obj
end
def method_missing(meth, *args, &blk)
@obj.class.superclass.instance_method(meth).bind(@obj).call(*args, &blk)
end
end
class Object
private
def sup
SuperProxy.new(self)
end
end
class A
def a
puts "In A#a"
end
end
class B<A
def a
end
def b
sup.a
end
end
B.new.b # Prints in A#a
https://stackoverflow.com/questions/-100007842
复制相似问题