防止子类覆盖 Swift 中的继承函数
Prevent subclasses from overriding inherited functions in Swift
有没有办法防止子类覆盖 Swift 中继承的函数?
看看 final
关键字。
根据文档,
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the final
modifier before the method, property, or subscript’s introducer keyword (such as final var
, final func
, final class func
, and final subscript
).
Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
您可以在 Swift 语言指南的 Inheritance section 底部找到更多信息(查找“防止覆盖”小节)。
有没有办法防止子类覆盖 Swift 中继承的函数?
看看 final
关键字。
根据文档,
You can prevent a method, property, or subscript from being overridden by marking it as final. Do this by writing the
final
modifier before the method, property, or subscript’s introducer keyword (such asfinal var
,final func
,final class func
, andfinal subscript
).Any attempt to override a final method, property, or subscript in a subclass is reported as a compile-time error. Methods, properties, or subscripts that you add to a class in an extension can also be marked as final within the extension’s definition.
您可以在 Swift 语言指南的 Inheritance section 底部找到更多信息(查找“防止覆盖”小节)。