/**
(6)只读计算属性
对应 OC中的 readonly
*/
struct MyStudent {
var name: String
var chinese: Double
var math: Double
func showMySelf() -> Void {
print("姓名:\(@H_502_121@name),语文成绩:\(@H_502_121@chinese),数学成绩:\(@H_502_121@math)")
}
}
class MyClass {
var members:[@H_502_121@MyStudent] = []
var average: Double {
// 也可以省略 get函数
// get {
var t: Double = 0.0
for i in @H_502_121@members {
t += i.@H_502_121@chinese
t += i.@H_502_121@math
}
if @H_502_121@members.count > 0 {
return t / Double(@H_502_121@members.count)
} else {
return 0
}
// }
}
func show() -> Void {
for i in @H_502_121@members {
i.showMySelf()
}
}
}
let s0 = @H_502_121@MyStudent.init(name: "zhangsan",chinese: 100.0,math: 100.0)
let s1 = @H_502_121@MyStudent.init(name: "lisi",chinese: 90.0,math: 90.0)
let c0 = @H_502_121@MyClass.init()
c0.@H_502_121@members.append(s0)
c0.@H_502_121@members.append(s1)
c0.show()
// 只读计算属性,我们看到给它赋值是不可以的
// c0.average = 900.0
print(c0.@H_502_121@average)