customize:override!

Said so many times rewriting,This time we have to rewrite treat seriously what method。

contract

We say,Inheritance is the equivalent of signing a contract,We inherited out of the subclass must comply with this contract,So even if you want to do something where no contract,Must comply with contract specifications,So,You override methods,It must also comply with the type of process。

We talked about the type of approach,It ()->() This represented。So,Overridden method must also comply with this type of argument that is receiving the return parameter type to be the same as the name of the same ...... Need I say more?

 

Rewrite

override ,I do not understand why the key words will be translated into overridden,Also fortunately they are not translated into the Swift in the override but translated into rewriting:

When the so-called override the automatic control system is receiving the accident alarm、More limited deviation、Abnormal signal failure,Override logic will perform automatic cut manual immediately depending on the cause of the accident、By priority、Priority Save、Prohibited by、Save ban and other logic functions,willSystem ConversionTo preprogrammed security status,And an alarm signal。

Here I translate it to rewrite,Although covering more appropriate - but rewrite it more intuitive。We have rewritten many ways the:

Use override to rewrite,In addition to ensure "compliance":The name of the same kind and outer,Access can only improve submissions can not be reduced,That is,If you add in passing to the parent class overrides a private prefix is ​​unacceptable:

Overridden method can not demote

Overridden method can not demote

but,If you want to try to increase permissions:

Although there is no sense,But acceptable

Although there is no sense,But acceptable

Here that does not make sense,Because the method of authority higher than that of the class - this method is not the same as access to。

Permissions issue

Unfortunately our syllabus did not include permission to this part of the course,Then I come here simply mention:

In Swift we have three permission:

  • public
  • internal
  • private

A front and a rear we know everything,What is it that the middle? - In fact, it is the default,If you do not declare privileges prefix,Then the default is internal。

public not to say,Ye Hao understand private,Private diary。Range is the range of your internal development of a project,Other projects can not be accessed。

Unrelated stuff

If you forget to write the override keyword when overwriting a method,It will prompt you to Xcode,But if you do not care about the same time, wrong type of method - then congratulations,The compiler will not complain,But rather that you override this method。

It is nothing to do with inheritance,Just another method has exactly the same way as the parent class name for the multi-state is nothing more ......,The compiler can still use the function to distinguish between these two types of methods with the same name - just like people always have the same name as。

For overloaded methods,You can understand the two methods just happen the same name Bale,Polymorphic in still calls the parent class,If you are alone call,You will see this prompt the:

Screenshot 2015-08-06 5.19.21 PM

The method of the same name by chance

So for you to access it casual。

After-school title

People do not take into account the fundamental,Not stay。

Original article written by Gerber drop-off:R0uter's Blog » customize:override!

Reproduced Please keep the source and description link:https://www.logcg.com/archives/1123.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 *