Swift static attributes

Source: Internet
Author: User

Swift static attributes
Before introducing static attributes, let's take a look at the design of a class. There is an Account (bank Account) class. Assume that it has three attributes: amount (Account amount) and interestRate (interest rate) and owner (account name ). In these three attributes, amount and owner will vary from person to person. Different accounts have different contents, and all accounts have the same interestRate.
The amount and owner attributes are related to individual accounts and are called instance attributes. The interestRate attribute has nothing to do with the individual, or is shared by all individual accounts. This attribute is called a static attribute or a type attribute.
Static attributes can be defined for all three object-oriented types (struct, enumeration, and class). Their syntax formats are as follows:

Struct name {① static var (or let) Storage attribute = xxx ② ...... static var calculation attribute name: attribute data type {③ get {return calculated attribute value} set (new attribute value ){......}}} enum enumeration name {④ static var (or let) Storage attribute = xxx ⑤ ...... static var calculation attribute name: attribute data type {6 get {return calculated attribute value} set (new attribute value ){......}}} class Name {7 ...... class var Calculation attribute name: attribute data type {calculated get {return calculated attribute value} set (new attribute value ){......}}}


In the above Code, line ① defines the struct, which can define static storage and computing attributes. Code ② defines the static storage attribute and declares the keyword static. This attribute can be a variable attribute or a constant attribute. The Code in line ③ defines the static computing attribute. The keyword used for the declaration is static, and the calculation attribute cannot be a constant. Here, it can only be a variable. The static computing attribute of the struct can also be read-only. The syntax is as follows:
Static var calculation attribute name: attribute data type {
Value of the calculated return Value
}
The forth row defines enumeration. Instance Storage attributes cannot be defined in enumeration, but static storage attributes or static computing attributes can be defined. The syntax for defining enumeration static attributes is exactly the same as that for defining struct static attributes.
Row 7 defines the class. The class can define the Instance Storage attribute, but cannot define the static storage attribute. Class. The keyword used for the declaration is class, which is different from the declaration of struct and enumeration.
We have summarized the above descriptions, as shown in the table below.
It indicates that you cannot access instance properties (including storage and computing properties) in static computing properties, but you can access other static properties. You can access instance attributes or static attributes in instance computing attributes.
I. structure static attributes
The following describes an example of static attributes of the Account structure:
Struct Account {var amount: Double = 0.0 // Account amount var owner: String = // Account name static var interestRate: Double = 0.668 // interest rate ① static var staticProp: double {② return interestRate * 0000000_000} var instanceProp: Double {③ return Account. interestRate * amount} // access the static attribute println (Account. staticProp) ④ var myAccount = Account () // access instance attributes myAccount. amount = 0000000_000 ⑤ // access the static property println (myAccount. instanceProp) ⑥


The above Code defines the Account structure. The Code in line ① defines the static storage attribute interestRate, and the code in line ② defines the static computing attribute staticProp, you can access static attributes such as interestRate In the attribute body. The Code in line ③ defines the instance computing attribute instanceProp, which can access the static attribute interestRate in its attribute body. The access method is "type name. Static attribute", such as Account. interestRate. Line 4 of the Code is also the access static attribute, and the access method is also "type name. Static attribute ".
The Code in line 5 and line 6 is used to access the instance attributes. The access method is "instance. instance attributes ".
Ii. Enumeration of static attributes
Let's take a look at an example of static attribute enumeration for an Account:
Enum Account {case Bank of China ① case Industrial and Commercial Bank of China case China Construction Bank of China ② static var interestRate: Double = 0.668 // interest rate ③ static var staticProp: double {④ return interestRate * 0000000_000} var instanceProp: Double {⑤ switch (self) {⑥ case Bank of China: Account. interestRate = 0.667 case Industrial and Commercial Bank of China: Account. interestRate = 0.669 case China Construction Bank: Account. interestRate = 0.666 case Agricultural Bank of China: Account. interestRate = 0.668} 7return Account. interestRate * 0000000_000 limit} // access the static attribute println (Account. staticProp) using var myAccount = Account. industrial and Commercial Bank of China // access the instance property println (myAccount. instanceProp) created


The above Code defines the Account enumeration type, in which ~ ② The line of code defines the four enumerated members. Line ③ defines the static storage attribute interestRate. Line ④ defines the static computing attribute staticProp, which can access static attributes such as interestRate in its attribute body. The fifth line of code defines the instance computing attribute instanceProp, wherein the sixth ~ 7. The line of code uses the switch statement to determine the value of the current instance and obtain different interest. Line 6 uses self, which indicates the current instance itself. The code in the nth line returns the calculation result.
The first line of code is to access static properties. The first line of code is to access the instance attributes.
The running result is as follows:
668000.0
669000.0
Iii. Static attributes
The following describes an example of static attributes of the Account class:
Class Account {① var amount: Double = 0.0 // Account amount var owner: String = // Account name var interestRate: Double = 0.668 // interest rate ② class var staticProp: double {③ return 0.668*0000000_000} var instanceProp: Double {④ return self. interestRate * self. amount ⑤} // access the static property println (Account. staticProp) ⑥ var myAccount = Account () // access the instance property myAccount. amount = 0000000_000 // access the static property println (myAccount. instanceProp) 7


The above Code defines the Account class in line ①, and the code in line ② defines the storage attribute interestRate. Note that the static storage attribute cannot be defined in the class. The Code in line ③ defines the static computing attribute staticProp, And the keyword is class. Line 4 defines the instance computing attribute instanceProp. In line 5, the Code accesses the instance attributes interestRate and amount. You can add "self. ", self refers to the current instance itself. Line 6 of code also accesses static properties. The Code in line 7 is used to access instance attributes.

 

 

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.