因为我们在java中使用“final”关键字来声明一个类不可扩展。我们如何在 Ruby 中实现这一目标?有什么办法吗?
最佳答案
假设您想阻止子类化,您可以从 inherited
中引发
异常。回调:
class Foo
def self.inherited(subclass)
raise "Can't inherit Foo class"
end
end
class Bar < Foo ; end
#=> RuntimeError (Can't inherit Foo class)
关于ruby - 有没有办法在 ruby 中指定类不可扩展?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56271136/