Class 在 class 内部具有获取和设置访问权限但在 class 外部仅在 Swift 中具有访问权限的变量?
Class Variable that have get and set access inside class but outside class only get access in Swift?
例如:-
在 Player Class 中,Score 应该在 Class 内部具有 get 和 set 访问权限,但在 class 外部 score 应该只读 属性.
Class Player{
var score : Int //get and set
init(score : Int){
self.score = score
}
func printScore(){
print(score)
}
func updateScore(by value: Int){
self.score += value
}
}
let player1 = Player(score : 30)
print(player1.score) //=> should be allowed
player.score = 100 //=> should not be allowed
这由 private(set)
访问控制处理:
private(set) var score : Int
有关这些访问控制的完整详细信息,请参阅 Swift 编程语言中的 Getters and Setters。
例如:- 在 Player Class 中,Score 应该在 Class 内部具有 get 和 set 访问权限,但在 class 外部 score 应该只读 属性.
Class Player{
var score : Int //get and set
init(score : Int){
self.score = score
}
func printScore(){
print(score)
}
func updateScore(by value: Int){
self.score += value
}
}
let player1 = Player(score : 30)
print(player1.score) //=> should be allowed
player.score = 100 //=> should not be allowed
这由 private(set)
访问控制处理:
private(set) var score : Int
有关这些访问控制的完整详细信息,请参阅 Swift 编程语言中的 Getters and Setters。