Original articles, welcome reprint. Reprint Please specify: Dongsheng's Blog
I'll start by designing a class: There's a Account(bank account) class, assuming it has3Properties:Amount(account amount),InterestRate(interest rate) andowner(account name).
in this3Properties,Amountand theownerWill vary from person to person, the contents of different accounts are different, and all accountsInterestRateare the same.
Amountand theownerattribute is related to the account individual, calledInstance Properties. InterestRateattribute is not related to an individual, or is shared by all account individuals, this attribute is calledStaticPropertiesorType Property.
Object-oriented types (structs, enumerations, and classes) can all define static properties, and their syntax formats are as follows:
struct structure name {// define structure, static storage properties and static calculation properties can be defined in the structure
static var (or let) storage attribute = "xxx"
...
static var computed attribute name: attribute data type {
get {
return calculated attribute value
}
set (new attribute value) {
...
}
}
}
enum enum name
static var (or let) storage attribute = "xxx"
...
static var computed attribute name: attribute data type {
get {
return calculated attribute value
}
set (new attribute value) {
...
}
}
}
class class name {// Define class, not only instance storage attributes can be defined in the class, but also static storage attributes
static
var (or let) storage attribute = "xxx"
...
class (or static) var computed attribute name: attribute data type {
get {
return calculated attribute value
}
set (new attribute value) {
...
}
}
}
struct static computed properties can also be read-only, with the following syntax:
static var computed attribute name: attribute data type {
return calculated attribute value
}
See a Account examples of struct static properties:
struct Account {// Define Account Structure
var amount: Double = 0.0 // account amount
var owner: String = "" // account name
static var interestRate: Double = 0.0668 // Define the static storage property interestRate interest rate
static var staticProp: Double {// Define static calculation property staticProp
return interestRate * 1_000_000
}
var instanceProp: Double {// Define instance calculation properties instanceProp
return Account.interestRate * amount
}
}
// Access static properties
print (Account.staticProp)
var myAccount = Account ()
// Access instance properties
myAccount.amount =
1_000_000
// Access static properties
print (myAccount.instanceProp)
Welcome to follow Dongsheng Sina Weibo @tony_ Dongsheng.
Learn about the latest technical articles, books, tutorials and information on the public platform of the smart Jie classroom
More ProductsIOS,Cocos, mobile Design course please pay attention to the official website of Chi Jie Classroom:http://www.zhijieketang.com
Smart-Jie Classroom Forum Website:http://51work6.com/forum.php
Swift 2.0 Learning Notes (Day 34)--What is the static property?