Swift 2.0 Study Notes (day 36)--static method

Source: Internet
Author: User
Tags instance method

Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog

static methods are similar to static properties,Swifta static method , also known as a type method , is defined in. Static methods are defined in the same way as static properties, and the keywords used by the static methods of enumerations and structs areStaticthe keywords used by class static methods areClassorStatic, If you use Staticis defined, themethod cannot be overridden in a subclass ( Override); If you useclassis defined, themethod can be overridden by a quilt class.

static method of structure body

Look at an example of a struct static method, with the following code:

        struct account {    var owner:string = "Tony"       & nbsp;        //Declare instance properties account name     static var interestrate:double = 0.0668 //Declaration static property rate     static func Interestby (amount:double), Double {//define static method   & nbsp;     return interestrate * amount   }    func messageWith ( amount:double), String {     //Defining instance methods         Let Interest = Account.interestby (amount)         return "\ (self.owner) interest is \ ( Interest) "   }}//call static method print (Account.interestby (10_000.00)) var myAccount = account ()     //Invoke instance method print (Myaccount.messagewith (10_000.00))  

Enumerate static methods

Look at an example of an enumeration of static methods, with the following code:

enum account {         Case BOC     case ICBC     case China Construction Bank      Case Agricultural Bank of China         static var interestrate:double = 0.0668  //Declare static property interest rate  & nbsp;      static func Interestby (amount:double), Double {   //define static method  & nbsp;      return interestrate * amount   }} //call static method print ( Account.interestby (10_000.00))   //Call static method  

As can be seen from the example, there is no difference in the use of static methods for structs and enumerations.

Class static methods

Look at a class static example of the method, the code is as follows:

class account {         var owner:string = "Tony"                  //account name    //can be converted to static    class Func Interestby ( amount:double), Double {//Use the keyword class to define static methods         return 0.08886 * Amount    }} //Call static method print (Account.interestby (10_000.00))    //Call static method  


Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.

Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom

More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php



Swift 2.0 Study Notes (day 36)--static method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.