为什么在Swift中不使用结构体的开放关键字?

huangapple go评论61阅读模式
英文:

Why you not use an open keyword with structs in swift?

问题

到处都在谈论只有 open/public 关键字的 classes,所以如果 Swift 的专家能够提供带有示例的令人满意的答案,那将是很棒的。

例如:我们可以在类中这样做,但在结构体中不要这样做!

open class Animal {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}
英文:

everywhere talk about only classes with open/public keyword so it would be great if swift experts can provide satisfying answers with an example.

for eg: we can do this in class but don't do it in the struct!

open class Animal {
    var name: String
    var age: Int
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}

答案1

得分: 3

open与结构体无关,因为它们不具有继承性。public与模块外部的可见性相关,因此适用于所有类型。

英文:

open is irrelevant to structs, since they don't have inheritance. public relates to visibility from outside the module, so it is relevant to all types.

huangapple
  • 本文由 发表于 2023年2月6日 18:41:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定