[swift] 71. get computed property

RudinP·2023년 10월 26일
0

Study

목록 보기
98/227

get computed property

  • getter 라고 생각하면 됨
  • 멤버변수의 값을 참조하여 특정 값을 리턴하는 멤버변수
  • getter인 경우 굳이 get을 쓰지 않고 생략해도 된다.
    • setter을 같이 쓸 경우에만 명시하기 위해 get을 씀
class Sword{
	var durability: Int = 100
    //getter
    var itemInfo: String{
    	switch durability{
        case 50...100: "명검"
        case 10...49: "쓸만함"
        default: "슬슬 고쳐라"
        }
    }
    
    func attackMonster{
	self.durability -= 50
	}
}

let sword = Sword()
sword.attackMonster()
sword.itemInfo // "명검"

sword.attackMonster()
sword.itemInfo // "쓸만함"
profile
곰을 좋아합니다. <a href = "https://github.com/RudinP">github</a>

0개의 댓글