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" // Declaring instance attribute account name static var interestrate: double = 0.0668 // Declaring a static property interest rate static func interestby (amount: Double) -> double {//Defining static Methods return Interestrate * amount } func messagewith (amount: double) -> string { // Defining instance Methods let interest = account.interestby (amount) return "\ (Self.owner) interest is \ (interEST) " }} //calls the 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.06 68//Declare static property rate static Func Interestby (amount:double), Double {//define static method return InterestRate * Amoun T}}//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:do uble), Double {//Use keyword class to define 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
650) this.width=650; "title=" 00.png "src=" http://s1.51cto.com/wyfs02/M01/7C/BF/wKiom1bXmlyyvPsYAAAs2MBEZnc731.png "alt=" Wkiom1bxmlyyvpsyaaas2mbeznc731.png "/>
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
This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1746990
Learn notes from scratch (Day 36)--Static methods