https://swift.gg/2016/01/11/public-properties-with-private-setters/
通过private(set)
修饰,可以将属性的setter
方法设置为私有的。可以使得代码更加简洁。
public class Circle {
public private(set) var area: Double = 0
public private(set) var diameter: Double = 0
public var radius: Double {
didSet {
calculateFigures()
}
}
public init(radius:Double) {
self.radius = radius
calculateFigures()
}
private func calculateFigures() {
area = M_PI * radius * radius
diameter = 2 * M_PI * radius
}
}