Initializer

At the end of the previous lesson,We finally made clear the process of creating an object,Then,Speaking create an object looks like is to call a method,This method may in the end what is it?

Initializer

Not wrong,When we initialize a class object when,We did call a method - initializer。

Initializer method is actually a special provision of the,It is possible for the class to initialize。

Let's imagine,If no initializer,So we call the class will certainly be stereotyped,Every time they have to create an object,Then modify its properties?

Ok,I admit that the current situation we are doing。

In short,We have a way to initialize an object at the same time according to different attributes to initialize the object,This is dependent on the initialization。

The default initializer

Our previous classes do not provide initializers,But because we write is a base class,The compiler will automatically help us to write a,This initialization is just the way we declare to all property assignment Bale,So when will we create objects using empty brackets - because there is no reception parameters initializer!

Let us look at the way the default initialization (not actually look like,But we can write exactly the same as a default initialization and initialization is to?)

Features

All right,Answers have been doubts in our minds,So this time again to re-learn what it constructor:It allows you to initialize an object at the same time to pass some parameters to help you initialize an object。

For chestnuts:

Look,This time we can directly declare objects of different states!

……and many more! This is good,But if I do not know the amount of ammunition it? You can not have a default? For example, the number of handgun ammunition ships are 8 sent this?

There must be something default

of course,We can also write in more than one class inside the initialization,Such methods have multiple initialization - this is allowed。

of course,If you do not want to do,It can also be used to initialize a convenient device to reuse the same initializer,We look at it is how it works:

With respect to the initializer,Convenient initializer to add convenience prefix,And finally it calls initializer,I do not say,Look at the code can understand how it works in the。

Overloaded initializer

Having a convenient initializer,Then we go back and see if you want to reload multiple initializers,There is no requirement:

Overloaded initialization can not be the same,That label、Parameters、There must be the same as the order is different,So as to compile。

Such two initializers although the same label but different,This is possible。

Initializer and Inheritance

Subclass objects can not be in the parent class is not initialized to the situation created - this is a paradox。So,If your class is a subclass,So we must first write initializer initializer parent class to call again - if there are parameters to pass parameters to have people be able to initialize。

If your parent is inherited from other classes,He initializer certainly this,So in the end it will call to initialize the beginning AnyObject,Such,Starting from the top of the inheritance tree down to a level initialization,Initialization you finalize this subclass。

Everyone talks about this process is often referred to "a constructor chain" - Well,Among the Swift,We call it "initializer",So this is the technical term "initialization chain" so。

Call the parent class's initialize

We use super.init() Way to call the parent class's initializer,If the same type initialization,You have to give it to rewrite out - use the override prefix。

so,If your class is inherited from the parent class,That's when I remember to write initialization initializer first call the parent class。

If the parent class initialization has parameters?

Then we need to write a receive these parameters initializers,Then initializer inside pass parameter initializer parent class:

 

Original article written by LogStudio:R0uter's Blog » Initializer

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1134.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *