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