前端之家收集整理的这篇文章主要介绍了
swift #6 类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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)
原文链接:https://www.f2er.com/swift/326525.html