Swift 2.0 Learning Notes (Day 34)--What is the static property?

Source: Internet
Author: User



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?


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.