Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

结构 struct 只能从协议继承 #8

Open
jaywcjlove opened this issue Apr 26, 2021 · 0 comments
Open

结构 struct 只能从协议继承 #8

jaywcjlove opened this issue Apr 26, 2021 · 0 comments
Labels

Comments

@jaywcjlove
Copy link
Owner

结构只能从协议继承(如果正确的话)。 不能从基本结构继承,所以您不能做

struct Resolution {
    var width = 0
    var height = 0
}

struct MyStruct: Resolution { ... }  // ERROR!

因此,有两个选择。 第一种是改用类。 第二个是重构代码以使用协议。

因此,有一些常用方法,则可以执行以下操作:

protocol PixelContainer {
   var width: Int { get }
   var height: Int { get }
}

extension PixelContainer {
    var count: Int { return width * height }
}

struct Resolution: PixelContainer {
    var width = 10
    var height = 20
}

let numPixels = Resolution().count  // Legal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant