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

Source: Internet
Author: User
Tags class definition

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

Static methods are similar to static properties, and a static method , also known as a type method , is defined in Swift. Static methods are defined in the same way as static properties, where the keyword used by the enumeration and static methods of the struct is static, the keyword used by the class static method is class or static, and if the static definition is used, the method cannot be overridden in a subclass (override) If you use the class definition, the method can be overridden by the quilt class.

static method of structure body

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

structAccount {varOwner:string ="Tony"                //declaring instance attribute account name    Static varInterestrate:double =0.0668  //declaring a static property interest rate    StaticFunc Interestby (amount:double), Double {//Defining static Methods        returnInterestRate *Amount} func messagewith (amount:double)-String {//Defining instance MethodsLet interest=Account.interestby (amount)return "\ (self.owner) interest is \ (interest)"           }} //calling a static methodPrint (Account.interestby (10_000.xx)) varMyAccount =Account ()//Invoking instance methodsPrint (Myaccount.messagewith (10_000.xx))

Enumerate static methods

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

enumAccount { CaseBank of China CaseICBC CaseChina Construction Bank CaseAgricultural BankStatic varInterestrate:double =0.0668   //declaring a static property interest rate    StaticFunc Interestby (amount:double), Double {//Defining static Methods        returnInterestRate *Amount}} //calling a static methodPrint (Account.interestby (10_000.xx))//calling a 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 an example of a class static method with the following code:

classAccount {varOwner:string ="Tony"                 //Account name//can be replaced by static    classFunc Interestby (amount:double), Double {//define a static method using the keyword class        return 0.08886*Amount}} //calling a static methodPrint (Account.interestby (10_000.xx))//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 Products iOS, Cocos, mobile design courses please pay attention to the official website of Chi Jie Classroom: http://www.zhijieketang.com
Luxgen 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.