Before we introduce static properties, let's look at the design of a class that has 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.
There are 3 object-oriented types (structs, enumerations, and classes) that can define static properties, and their syntax formats are as follows:
[HTML]View Plaincopy
- struct struct body name {①
- static VAR (or let) store property = "xxx" ②
- ......
- Static Var computed property name: property data type {③
- get {
- Return computed property value
- }
- Set (new property value) {
- ......
- }
- }
- }
- Enum enum name {④
- static VAR (or let) store property = "xxx" ⑤
- ......
- Static Var computed property name: property data type {⑥
- get {
- Return computed property value
- }
- Set (new property value) {
- ......
- }
- }
- }
- Class Name {⑦
- ......
- Class Var computed property Name: property data type {⑧
- get {
- Return computed property value
- }
- Set (new property value) {
- ......
- }
- }
- }
In the preceding code, line ① is the definition of a struct, where static storage properties and computed properties can be defined. The ② code defines a static storage property, and the Declaration keyword is static, which can be a variable property or a constant property. The ③ line code defines a static computed property, declares that the keyword used is static, the computed property cannot be a constant, and only the variable. struct static computed properties can also be read-only, with the following syntax:
Static Var computed property name: property data type {
Return computed property value
}
The ④ is defined as an enumeration in which an instance store property cannot be defined, but static storage properties can be defined, or static computed properties can be defined. Defining enumeration static properties is exactly the same syntax as defining a static property of a struct, which is not discussed here.
Line ⑦ is a definition class in which you can define instance store properties, but you cannot define static storage properties. A static computed property can be defined in a class. The keyword used by the declaration is class, which differs from the declaration of structs and enumerations.
We summarize the above instructions, as shown in the table below.
Tip You cannot access instance properties (including storage properties and computed properties) in a static computed property, but you can access other static properties. Instance properties can be accessed in the instance calculation properties, and static properties can also be accessed.
Static property of structure body
Let's look at an account structure static Property Example:
[HTML]View Plaincopy
- 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 * 1_000_000
- }
- var instanceprop:double {③
- Return Account.interestrate * Amount
- }
- }
- accessing static properties
- println (Account.staticprop) ④
- var myAccount = Account ()
- accessing instance Properties
- Myaccount.amount = 1_000_000⑤
- accessing static properties
- println (Myaccount.instanceprop) ⑥
The code above defines the account structure, where the ① line code defines the static storage property interestrate, and the ② line code defines the static computed property Staticprop, which can access static properties such as InterestRate in its property body. The ③ code defines the instance calculation property Instanceprop, which can access the static property interestrate in its property body, accessed in the form "type name. Static property", such as Account.interestrate. The ④ Line code is also an Access static property, accessed in the same way as "type name. Static Property".
The ⑤ line and the ⑥ line code are access instance properties, accessed by "instance. Instance Properties".
Two Enumerate static properties
Let's look at an account enumeration static Property Example:
[HTML]View Plaincopy
- 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 Var staticprop:double {④
- return interestrate * 1_000_000
- }
- var instanceprop:double {⑤
- Switch (self) {⑥
- Case Bank of China:
- account.interestrate = 0.667
- Case ICBC:
- account.interestrate = 0.669
- Case China Construction Bank:
- account.interestrate = 0.666
- Case Agricultural Bank of China:
- account.interestrate = 0.668
- }⑦
- Return account.interestrate * 1_000_000⑧
- }
- }
- accessing static properties
- println (Account.staticprop) ⑨
- var myAccount = Account . ICBC
- accessing instance Properties
- println (Myaccount.instanceprop) ⑩
The code above defines the account enumeration type, where the ①~② line code defines the 4 members of the enumeration. The ③ code defines the static storage property interestrate, and the ④ line code defines the static computed property Staticprop, which can access static properties such as InterestRate in its property body. The ⑤ code defines the instance calculation property Instanceprop, where the ⑥~⑦ line code uses the switch statement to determine the value of the current instance, to obtain different interest, and the ⑥ line code uses self, which refers to the current instance itself. The ⑧ Line code is the result of the return calculation.
The ⑨ line code is the Access static property. The ⑩ line code is the Access instance property.
The results of the sample run are as follows:
668000.0
669000.0
Third, class static properties
Let's look at an account class static Property Example:
[HTML]View Plaincopy
- 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 * 1_000_000
- }
- var instanceprop:double {④
- Return self.interestrate * Self.amount⑤
- }
- }
- accessing static properties
- println (Account.staticprop) ⑥
- var myAccount = Account ()
- accessing instance Properties
- Myaccount.amount = 1_000_000
- accessing static properties
- println (Myaccount.instanceprop) ⑦
The above Code section ① defines the account class, and the ② line code defines the storage property interestrate, noting that static storage properties cannot be defined in the class. The ③ code defines the static computed property Staticprop, which is the class keyword. The ④ code defines the instance calculation property Instanceprop, which accesses the instance properties InterestRate and amount in the ⑤ line code, accessing the instance properties of the current object can be added "self" in front of the property. The ⑥ Line code is also an Access static property. The ⑦ line code is the Access instance property.
For more information, please visit the first Swift book "Swift Development Guide" book Exchange discussion website: http://www.51work6.com/swift.php Welcome to join Swift Technical discussion group: 362298485
Welcome to Luxgen iOS Classroom public Platform
Swift Static properties