similar to static properties, a static method, also known as a type method, is defined in Swift, which refers to enumerations, structs, and classes. A static method defines a method that is similar to a static property, where the keyword used by the enumeration and static methods of the struct is static, and the keyword used by the static method of the class is class.
A static method in a structural body
Let's look at an example of a struct static method, with the following code:
struct Account { var owner:string = "Tony" //username ① static var interestrate:double = 0.668 //interest rate ② STA Tic func Interestby (amount:double), Double {③ return interestrate * Amount } func Messagewith (amount: Double) String {④ var interest = Account.interestby (amount) return "\ (self.owner) interest is \ (Interest)" }} Call the static method println (Account.interestby (10_000.00)) ⑤var MyAccount = account () ⑥//Call instance method println (Myaccount.messagewith (10 _000.00)) ⑦
The code above is defining the account structure, and the ① code declares the instance property owner. The ② line code declares the static property interestrate. The ③ line code defines a static method Interestby, which, like static computed properties, cannot access instance properties or instance methods.
Line ④ is the definition of the instance method Messagewith, the instance method can access the instance properties and methods, and also can access the static properties and methods. In this method we use the Self.owner statement, where self is a hidden property that refers to the current type instance, and generally we do not use it unless the property name conflicts with the variable or constant name.
TipsSelf is also used in Swift's static methods, which is not allowed in other object-oriented computer languages. At this point, self represents the current data type and does not represent an enumeration, struct, or instance of a class.
Ii. static methods in enumerations
Let's 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.668 Interest rate ① static func Interestby (amount:double), Double {② return interestrate * Amount }}//call to Method println (Account.interestby (10_000.00)) ③
The code above is to define the account enumeration, and the ① line code declares the static property interestrate. The ② line code defines a static method Interestby, which, like static computed properties, cannot access instance properties or instance methods. The ③ line code is called by the static method.
As can be seen from the example, there is no difference in the use of static methods for structs and enumerations.
Third, static methods in class
Let's look at an example of a class method, with the following code:
Class Account { var owner:string = "Tony" //Username class func Interestby (amount:double), Double {① R Eturn 0.8886 * Amount }}//call static method println (Account.interestby (10_000.00)) ②
The code above defines the account class, and the ① code uses the keyword class to define a static method Interestby, which is similar to a static computed property, and it cannot access instance properties or instance methods. The ② line code is called by the static method.
For more information, please follow the first swift book "Swift Development Guide" in China Book Exchange Discussion website: http://www.51work6.com/swift.php Welcome to the Swift Technical Discussion group: 362298485
Welcome to Luxgen iOS Classroom public Platform
Swift static method