protocol:Allowed instantiated class and method must be rewritten

Rewriting said method,We come back to look at the inheritance tree:

Class inheritance tree

Class inheritance tree

This should look okay,We can create an instance of the sword,Create an instance of a pistol ......

But if I have to write it?

So the question is:excavator……

Do not,Our problem is "weapon" in the end is what?

We say "animal",Man is an animal,Birds are animals,dog、Cats are animals,That there is no animal animals?

The answer is no - the animal is an abstract vocabulary。Same,As the successor to the top of the tree class,It must have been an extremely abstract guy,From here instantiated out - not,It should never be instantiated!

So,We will be able to find a way to make the class can be inherited,It can not be initialized。

Abstract class

For us this is an inheritance tree,"arms"、"Cold weapon"、"Hot weapon"、"firearms"、"Remote" and these classes should not be instantiated,They are abstract things,So we take these classes as an abstract class to be treated,It must be inherited to use,And inside the method it must be rewritten to call!

That is only the provisions of the framework contract,To the subclasses themselves to achieve the contract。

The first method

byspeakInitializes the device private mess,Thus we call a class to instantiate it when,The compiler can not find the device initialization - we have not yet talked about initializers,And there is no written,Now you just need to know,We do not write it yourself compiler will help you achieve a basic,If you write,The compiler will use your。

In short,We use this method implements this class can not be initialized。


To test this approach,Not in the playground,New projects must use a different file to manipulate the job!

The second method

Another thought,Replace the class to declare the agreement - because the agreement is inherently can not be instantiated ......


Statement agreements and declarations about the same category,We are capitalized at the beginning of naming,But class protocol to be replaced 。

In the agreement can not be set in the property value method can not be realized,Only write the same specification as placeholders,The subclasses inherit the time will be required to cover these members and。

Let's take a chestnut

Although such an agreement into,But it does make this class can not be instantiated,Of course,Inherited from a subclass of this agreement do not need to override the method - because the method itself is not implemented,We just need to realize their a。

Here we say less stringent,For the terms of the agreement can not be inherited,Nor is the relationship between the parent class and subclass - currently you can understand and use,Because for Swift itself,And there is no concept of an abstract class。

There can not be covered and inheritance solution?

Use prefix marks a final class or method,This class or method used to declare a "final" version,It can not be inherited or rewritten。

Original article written by Gerber drop-off:R0uter's Blog » protocol:Allowed instantiated class and method must be rewritten

Reproduced Please keep the source and description link:

About the Author


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

Leave a Reply

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