【发布时间】:2021-09-30 20:41:47
【问题描述】:
是否可以在 Swift 5 中阻止子类的所有子类访问基类中的变量?请不要绕道协议解决方案。
class A { var a:String = ""}
class B : A {protected var a = ""}
class C : B
{
func use_a()
{
a = //compiler should complain here that 'a' is protected and cannot be modified
}
}
【问题讨论】:
-
编译器怎么会抱怨protected,这不是java?请发布真实代码。但总的来说,您永远不能降低属性的可访问性。
-
新行上的花括号...
标签: swift inheritance