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

Source: Internet
Author: User

original articles, welcome reprint. Reprint Please specify: Dongsheng's blog

static methods are similar to static properties,Swift a 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 are Staticthe keywords used by class static methods areclassorStatic , If you use Staticis defined, the Method cannot be overridden in a subclass ( Override); If you useclassis defined, the Method 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"                //Declare instance attribute accounts name    static var interestrate:double = 0.0668  //Declare static property Interest rate        static func Interestby (amount:double)-Double {//define static method         return InterestRate * Amount    }        func Messagewith (amount:double), String {      //Define instance method 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 Bank of China, ICBC case)        CCB case    abc        static var interestrate:double = 0.0668   //Declare static property rate static        func Interestby (amount:double), Double {    //define static method        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 a method, the code is as follows:

Class Account {        var owner:string = "Tony"                 //Username    //can be replaced by static    class Func Interestby (amount:double)- > Double {//Use the keyword class to define a static method        return 0.08886 * Amount    }}//Call static method print (Account.interestby (10_000.00))    Calling a 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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.