Learning Swift from scratch (day 34)--What is a 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 has 3 Properties: Amount (account amount), InterestRate (interest rate) and owner (account name).

in this 3 amount and owner interestrate are all the same.

Amount and the owner attribute is related to the account individual, 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:

struct struct Name {//define struct body, can define static storage property and static computed property in struct
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 Enumeration Name {// Define an enumeration, which cannot be used to define an instance store property, but can define a static storage property, or you can define a static computed property

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 class Name {//define classes in which you can define not only instance store properties, but also static storage properties
Staticvar (or let) store property = "XXX"
...
Class (or Static) var computed property name: property data type {
get {
Return computed property value
}
Set (new property value) {
...
}
}
}
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
}


See a Account examples of struct static properties:

struct Account {//Define account structure body

var amount:double = 0.0//Account Amount
var owner:string = ""//Account name

static var interestrate:double = 0.0668//define the stored property interestrate interest rate

static Var staticprop:double {//define statically computed properties Staticprop
return interestrate * 1_000_000
}

var instanceprop:double {//Define instance calculation properties Instanceprop
Return Account.interestrate * Amount
}
}

accessing static properties
Print (Account.staticprop)

var myAccount = account ()
accessing instance Properties
Myaccount.amount =1_000_000
accessing static properties
Print (Myaccount.instanceprop

)

Welcome to Dongsheng Sina Weibo @tony_

focus on the public platform Learn about the latest technical articles, books, tutorials information
                                650) this.width=650; "title=" 00.png "alt=" Wkiol1bwtamtwrmraaas2mbeznc576.png " Src= "Http://s3.51cto.com/wyfs02/M00/7C/AF/wKioL1bWTamTWrmRAAAs2MBEZnc576.png"/>
ios cocos http://www.zhijieketang.com
Smart-Jie Classroom Forum website: http://51work6.com/forum.php


This article is from the "Dongsheng-ios Technical Consultant" blog, make sure to keep this source http://tonyguan.blog.51cto.com/701759/1746605

Learning Swift from scratch (day 34)--What is a 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.