标签:静态方法
一、结构体中的静态方法
示例:
struct Account{
var owner:String = ""
static var interestRate : Double = 0.688
static func interestBy(amount:Double)->Double{
return interestRate * amount;
}
func messageWith(amount:Double)->String{
var interest = Account.interestBy(amount)
return("\(self.owner):\(interest)")
}
} 1、静态方法与静态计算属性相似,它不能访问实例属性或实例方法;
2、实例方法能访问实例属性和方法,也能访问静态属性和方法;
二、枚举中的静态方法
示例
enum Account{
case 中国银行
case 中国农业银行
case 中国工商银行
static interesetRate:Double = 0.668
static interestBy(amount:Double)->Double{
return interestRate * amount;
}
}三、类中的静态方法
使用class关键词定义静态方法。
本文出自 “平凡之路” 博客,请务必保留此出处http://linjohn.blog.51cto.com/1026193/1622299
标签:静态方法
原文地址:http://linjohn.blog.51cto.com/1026193/1622299