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:
[HTML]View Plaincopy print?
- struct Account {
- var owner: String = "Tony"//Account name ①
- static var interestrate: Double = 0.668//interest rate ②
- static func Interestby (amount:double)-> double {③
- Return InterestRate * Amount
- }
- Func Messagewith (amount:double)-> String {④
- var interest = Account.interestby (amount)
- Return "\ (self.owner) interest is \ (Interest)"
- }
- }
- Calling a static method
- println (Account.interestby (10_000.00)) ⑤
- var myAccount = Account () ⑥
- Invoking instance methods
- 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.
It is also possible to use self in a static method of Swift, 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:
[HTML]View Plaincopy print?
- Enum Account {
- Case Bank of China
- Case ICBC
- Case China Construction Bank
- Case Agricultural Bank of China
- static var interestrate: Double = 0.668//interest rate ①
- static func Interestby (amount:double)-> double {②
- Return InterestRate * Amount
- }
- }
- Calling a static 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:
[HTML]View Plaincopy print?
- Class Account {
- var owner: String = "Tony"//account name
- Class func Interestby (amount:double)-> double {①
- Return 0.8886 * Amount
- }
- }
- Calling a 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.
Swift static method