Swift 5. 让第一个 child class 访问 属性,而不是 child child class
Swift 5. Let first child class access a property, but not child of child class
是否可以阻止 child class 的所有 children 访问 Swift 5 中基数 class 中的变量?请不要 round-about 协议解决方案。
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
}
}
这是可能的,但只能通过使用文件结构来限定成员的范围。
文件 1:
class A {
fileprivate var a = ""
}
class B: A {
fileprivate override var a: String {
willSet { }
}
}
文件 2:
class C: B {
var a = 867_5309
func use_a() {
super.a // 'a' is inaccessible due to 'fileprivate' protection level
}
}
是否可以阻止 child class 的所有 children 访问 Swift 5 中基数 class 中的变量?请不要 round-about 协议解决方案。
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
}
}
这是可能的,但只能通过使用文件结构来限定成员的范围。
文件 1:
class A {
fileprivate var a = ""
}
class B: A {
fileprivate override var a: String {
willSet { }
}
}
文件 2:
class C: B {
var a = 867_5309
func use_a() {
super.a // 'a' is inaccessible due to 'fileprivate' protection level
}
}