Swift's initial "swift" construction process

Source: Internet
Author: User

The so-called construction process refers to a series of preparatory processes that are performed in creating an instance. For example, set an initial value for an attribute in an instance and perform other initialization work.
The construction process is implemented through constructors, in fact, each constructor can be considered as a function, but this function is to perform initialization.

1. Initial assignment of storage properties
As an example of instance creation of classes and structs, when creating classes and struct-body instances, you need to set a default value for the properties of classes and street titles in two ways:
1?? Set the default value when defining a property; 2?? Assigning an initial value in a constructor
Either way, their values are set directly and do not trigger any property observers.
So what is a constructor? In fact, in the inheritance of the above class we have already seen, is named after the keyword init instance method:
struct Rect {    var width:double    init () {        width = 10.0    }}


2, the construction process of custom
We have just used the default constructor, if we need to use some of our own input parameters when creating the instance, this time we need to use the custom construction process.
In fact, the construction process of customization is simply to change the non-parametric constructor into a parameter-containing constructor, the other is unchanged.
For example, look at the following examples:
struct Rect {    var width:double    var height:double = 10.0    init (Square square:double) {        width = square
   }    init (Rectangle rectangle:double) {        width = Rectangle    }    Func description () {        println ("width is \ (width) ")    }}var graph:rect = rect (square:10) var graph2 = rect (rectangle:20) graph.description () Graph2.descrip tion ()


In this example we provide 2 constructors, one to create a square, one to create a rectangle, hehe, it seems a little superfluous ....
When we create an instance, we can call different constructors to do different things.

3. Default constructor
Swift will provide a default constructor for any struct or base class for which all properties have provided default values and do not have any constructors defined by themselves, and this default constructor will simply create an instance where all property values are set to the default values.
such as the following:
struct Rect {    var width:double = 10.0    var height:double = 10.0}var Graph:rect = Rect ()


This rect does not have a constructor and the system provides a default constructor.
In addition to the default constructors mentioned above, the system can also use a member constructor to initialize a shortcut to the member properties in the new instance. Please refer to the Structural body section.

4. Constructor Agent
The so-called constructor proxy is that the constructor can be used to invoke other constructors to complete the partial construction of the instance, which can reduce code duplication between multiple constructors.
Because value types (structs and enumerations) do not support inheritance, the process of the constructor agent is relatively straightforward because they can only delegate tasks to other constructors provided by themselves. Class is different, it can be inherited from other classes, which means that all other inherited properties are properly initialized at the time of construction.
struct Rect {    var width:double = 10.0    var height:double = 10.0    init () {            }    init (h:double) {        s Elf.height = h        println ("H is \ (h)")    }    init (printh:double) {        self.init (h:printh)    }}var Graph:rec t = rect (printh:3.2) var graph2 = rect (h:4.3)


The struct provides 3 constructors, one is an empty init, no action is taken, one is passed in H, the value of H is printed, one is passed in Pirinth, and then the constructor of Init (h:double) is called to print out the value I entered.

5. Inheritance and construction process of class
All properties in a class include attributes that inherit from the parent class, and you must set the initial values during the construction process.
There are two constructors in a class, one for the specified constructor and one for the convenience constructor, all of which ensure that the properties stored in all instances of the class can get the initial value.
Specifies that the constructor is the primary constructor in the class, and that a specified constructor initializes all the properties provided in the class and initializes the parent class based on the constructor of the parent class that calls the parent class. Each class must have a specified constructor.

The convenience constructor is an auxiliary constructor, and we can define a convenience constructor to invoke the specified constructor in the same class and provide it with parameters and default values.



Continue next time.

Swift's initial "swift" construction process

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.