class TriangleAndSquare {
var triangle: EquilateralTriangle {
willSet {
square.sideLength = newValue.sideLength
println("--------")
}
didSet {
println("------++++++")
Square(sideLength: 50, name: "larger square")
}
}
var square: Square {
willSet {
println("++++++++++++")
triangle.sideLength = newValue.sideLength
}
didSet {
println("+++++----------")
EquilateralTriangle(sideLength: 150, name: "larger TriangleAndSquare")
}
}
init(size: Double, name: String) {
square = Square(sideLength: size, name: name)
triangle = EquilateralTriangle(sideLength: size, name: name)
}
}
var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
triangleAndSquare.square.sideLength
triangleAndSquare.triangle.sideLength
triangleAndSquare.square = Square(sideLength: 50, name: "larger square")
triangleAndSquare.square.sideLength
triangleAndSquare.triangle.sideLength
triangleAndSquare.triangle = EquilateralTriangle(sideLength: 150, name: "larger TriangleAndSquare")
triangleAndSquare.square.sideLength
triangleAndSquare.triangle.sideLength
原以为会死循环,可貌似没有,有清楚机制的吗?