我有一些基类 A 有一个不会被覆盖的方法。
class A
def dont_override_me
puts 'class A saying, "Thank you for not overriding me!"'
end
end
另一个类 B 扩展 A 并尝试覆盖该dont_override_me
方法。
class B < A
def dont_override_me
puts 'class B saying, "This is my implementation!"'
end
end
如果我实例化 B 并调用dont_override_me
,则将调用 B 类的实例方法。
b = B.new
b.dont_override_me # => class B saying, "This is my implementation!"
这是因为红宝石的特性。可以理解。
但是,如何强制基类方法dont_override_me
不能被它的派生类覆盖?我在 java 中找不到像final
ruby 这样的关键字。在 C++ 中,可以将基类方法设置为非虚拟的,以便派生类无法覆盖它们。我如何在红宝石中实现这一点?