class  Account {

    //属性 定义
    var firstName: String
    var lastName: String
    var balance: Double

    var rate = 0.0

    // 构造函数
    init(firstName:String , lastName:String , balance: Double){
        self.firstName=firstName
        self.lastName=lastName
        self.balance=balance
    }

    // 方便化的 构造函数
    convenience init (){
        self.init(firstName:"",lastName:"",balance:0.0 )
    }

    // 定义对象方法
    func   printBreakDown() {
        var balance = "\(self.balance)"
        println("printBreakDown ...\(self.firstName) \(self.lastName) \(balance)")
    }

    func interestOverYears(years: Int) ->(Double) {
        return 0
    }
}

//继承类
class CheckingAccount: Account {
    //重载构造函数
    override init(firstName: String, lastName: String, balance: Double) {
        super.init(firstName: firstName, lastName: lastName, balance: balance)
        self.rate = 4
    }

    //重构对象方法
    override func interestOverYears(years: Int) ->(Double) {
        return (rate * balance * Double(years)) /  100
    }
}


var account=Account(firstName: "kai", lastName: "wang", balance: 1233.3)
account.printBreakDown()

var checkAccount = CheckingAccount(firstName: "kai", lastName: "wanbg", balance: 1300.0)
checkAccount.interestOverYears(10)

hqman
198 声望8 粉丝

« 上一篇
swift #5 函数
下一篇 »
swfit #7 Tuple