有没有办法在 ruby 中指定 class 不可扩展?
is there a way to specify class unextendable in ruby?
因为我们使用 "final" 关键字在 java 中声明一个 class 不可扩展。我们如何在 ruby 中实现这一点?有什么办法吗?
假设您想要阻止子类化,您可以 raise
来自 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)
因为我们使用 "final" 关键字在 java 中声明一个 class 不可扩展。我们如何在 ruby 中实现这一点?有什么办法吗?
假设您想要阻止子类化,您可以 raise
来自 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)