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