Swift: Object-oriented (property)

Source: Internet
Author: User
Tags value of pi

The use of properties in Swift is much richer than the use of properties in Objective-c. Let me introduce each of them.

One, storage properties and deferred storage properties

The demo is as follows:

Class Dog {    var name = "Wang Choi"    init () {        println ("dog initialized")}}class person {let age    =    var name = "Ja CK "    var height = 0.0        lazy var dog = dog ()}var p = person () P.dog

in the code, the dog and person two classes are defined.

1. The name attribute in dog and the age,name,height in person are stored properties used to store the object's normal property values.

2. The dog attribute in person, which is also a storage property, except that its property value is a dog object, and the dog attribute is preceded by the Lazy keyword adornment, it becomes the deferred storage property.

3. Delayed Storage properties: The first time to use the time to initialize, can not act on the constants above, generally acting on the object above. When the above code var p = person () finishes executing, the dog property is not initialized. When the P.dog is executed, the dog is initialized with a dog object.

Second, calculate the attribute

The computed properties in Swift are similar to those in C # get;set;

The demo is as follows (calculates square perimeter);

Class Square {    var width:double = ten    var grith:double {        get{            return width * 4        }        set{            Widt h = Newvalue/4        }    }}var s = Square () s.width         //10s.grith     //40s.grith = +   //width:25

in the above example, width represents the edge length of the square, which is the storage property, and Grith represents the perimeter of the square, which is the computed attribute, and the Grith has a get and set two methods in the computed attribute, and the get means that the perimeter can be calculated based on the length of the edge , and set indicates that the edge length can be calculated based on the perimeter. It is necessary to note that the newvalue in the set method is the value passed by the external assignment to girth, such as the above S.grith = 100 after execution, the value of NewValue is 100, and this newvalue value is the system default variable, we can also define the variable, as follows:

Class Square {    var width:double = ten    var grith:double {        get{            return width * 4        }        set (myvalue) {
   width = Myvalue/4        }}}    

Note: The storage property must have data initialization for the property (unless the construction method is used, the next section is explained), and the computed attribute can be computed using the stored properties to get the final result.

third, type attribute

The type attribute is referred to as the class attribute.

1. Only the calculated attribute.

2. Regardless of the number of instances created, these instances share a single resource.

Class Square {    class var pi:double{        return 3.14    }}println ("value of PI: \ (SQUARE.PI)")

in the above example, pi is the type attribute, which is decorated with the class keyword. And when called, the direct "class name. Attribute name" is OK. The type attribute in the above is shorthand, and we can also write:

Class Square {    class var pi:double{        get{            return 3.14        }    }}
Iv. Property Monitor

1. If it is a computed attribute, listen directly in the Set method, similar to the KVO mode in the Objective-c listener property.

2. If the property is stored, we need to monitor it using Willset and Didset.

The demo is as follows:

Class Square {    var width:double = 0.0 {        willset{            println ("New value:\ (newvalue), current value:\ (width)") c4/>}        didset{            println ("Old Value:\ (OldValue), current value:\ (width)")        }    }}var s = Square () S.width = 100

Willset and Didset are not called during property initialization. When executed to code s.width = 100, the two methods of Willset and Didset are triggered, and the end result is:

New value:100.0, current value:0.0
Old value:0.0, current value:100.0

Five, Property rewrite

An inherited read-only property can be rewritten as a read-write property.

Class Animal {    var speed:int {        get {            return]        }    }}class dog:animal {    override var Speed:int {        get {            return        }        set {    }}}

you cannot rewrite an inherited read-write property as a read-only property (the following code, the dog class will error)

Class Animal {    var speed:int {        get {            return]        set {                }    }}class Dog:animal {    Override var Speed:int {        get {            return}        }    }

overrides of the stored property (the value of the final attribute is stored in the parent class)

Class Animal {    var speed:int = 0 {        Willset {            println ("Animal-willset")        }        didset {            println ("Ani Mal-didset ")        }    }}class dog:animal {    override var speed:int {        Willset {            println (" Dog-willset ") 
   }        didset {            println ("Dog-didset")        }    }}var d = Dog () d.speed = 10 final print result Dog-willsetanimal-willse Tanimal-didsetdog-didset

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Swift: Object-oriented (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.