假如某個員工薪水低於30K,就給員工30K。
我們先用Swift建立一個class叫做Employee
public class Employee { var Name : String = "" private var _Salary : Int = 0 var Salary : Int{ set { if newValue < 30000 { self._Salary = 30000 } else { self._Salary = newValue } } get { return self._Salary } } }
var emp : Employee emp = Employee() emp.Name = "boywhy" emp.Salary=23000 print("Name:\(emp.Name) Salary :\(emp.Salary)")
輸出結果為 Name:boywhy Salary :30000
沒有留言:
張貼留言