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