swift-static Method-Standby

Source: Internet
Author: User
Tags instance method

Static methods are similar to static properties, anda static method , also known as a type method , is defined in Swift. Static methods are defined in the same way as static properties, where the keywords used by the enumeration and static methods of the struct are static , the keyword used by the class static method is class or static, and if the static definition is used , the method cannot be overridden in a subclass ( override); If you use the class definition, the method can be overridden by the quilt class.  

static method of structure body

Look at an example of a struct static method, with the following code:

[HTML]View PlainCopyprint?
  1. struct Account {
  2. var owner: String = "Tony"//declaring instance attribute account name
  3. static var interestrate: Double = 0.0668//Declare static property interest rate
  4. static func Interestby (amount:double)-> Double {//define static method
  5. Return InterestRate * Amount
  6. }
  7. Func Messagewith (amount:double)-> String {//define instance method
  8. Let interest = Account.interestby (amount)
  9. Return "\ (self.owner) interest is \ (Interest)"
  10. }
  11. }
  12. Calling a static method
  13. Print (Account.interestby (10_000.00))
  14. var myAccount = Account ()
  15. Invoking instance methods
  16. Print (Myaccount.messagewith (10_000.00))

Enumerate static methods

Look at an example of an enumeration of static methods, with the following code:

[HTML]View PlainCopyprint?
  1. Enum Account {
  2. Case Bank of China
  3. Case ICBC
  4. Case China Construction Bank
  5. Case Agricultural Bank of China
  6. static var interestrate: Double = 0.0668//Declare static property interest rate
  7. static func Interestby (amount:double)-> Double {//define static method
  8. Return InterestRate * Amount
  9. }
  10. }
  11. Calling a static method
  12. 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 an example of a class static method with the following code:

[HTML]View PlainCopyprint?
    1. Class Account {
    2. var owner: String = "Tony"//account name
    3. Can be replaced by static
    4. Class func Interestby (amount:double)-> Double {//using the keyword class to define a static method
    5. Return 0.08886 * Amount
    6. }
    7. }
    8. Calling a static method
    9. Print (Account.interestby (10_000.00))//Call static method

swift-static Method-Standby

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.