码迷,mamicode.com
首页 > 其他好文 > 详细

静态方法

时间:2015-03-19 18:39:06      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:静态方法

一、结构体中的静态方法

        示例:

        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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!