Swift中类可以使用计算属性,即使用get和set来间接获取/改变其他属性的值
class student: NSObject {
var age = 1;
var num = 2;
var sum :Int{
get{
return age+num
}
set(op){
num = op-age
}
}
}
let astudent = student();
print("\(astudent.sum)");
astudent.sum=10;
print("\(astudent.num)");
网友评论