swift-Static Properties-Standby

Source: Internet
Author: User

Let me first design a class: There is an account (bank accounts) class, assuming it has 3 attributes:Amount (account amount),interestrate (interest rate), and owner (account name).

of These 3 attributes, amount and owner will vary from one account to another, and the contents of each account are different, and the interestrate of all accounts are the same.

The amount and owner properties are related to the account entity, called instance properties. The interestrate attribute is independent of the individual, or shared by all account individuals, which is called a static property or type attribute.

Object-oriented types (structs, enumerations, and classes) can all define static properties, and their syntax formats are as follows:

 
  1. struct struct Name {//define struct body, can define static storage property and static computed property in struct
  2. static VAR (or let) store property = "XXX"
  3. ...
  4. Static Var computed property name: property data type {
  5. get {
  6. Return computed property value
  7. }
  8. Set (new property value) {
  9. ...
  10. }
  11. }
  12. }
  13. Enum enum Name {///define enumeration in which an instance store property cannot be defined, but you can define a static storage property, or you can define a static computed property
  14. static VAR (or let) store property = "XXX"
  15. ...
  16. Static Var computed property name: property data type {
  17. get {
  18. Return computed property value
  19. }
  20. Set (new property value) {
  21. ...
  22. }
  23. }
  24. }
  25. Class class Name {//define classes in which you can define not only instance store properties, but also static storage properties
  26. Staticvar (or let) store property = "XXX"
  27. ...
  28. Class (or Static) var computed property name: property data type {
  29. get {
  30. Return computed property value
  31. }
  32. Set (new property value) {
  33. ...
  34. }
  35. }
  36. }


struct static computed properties can also be read-only, with the following syntax:

 
    1. Static Var computed property name: property data type {
    2. Return computed property value
    3. }

Look at an account struct static Property Example:

)

swift-Static Properties-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.