[OC learning-12] What is class inheritance? Why inherit? And precautions in inheritance

Source: Internet
Author: User

(1) how to implement class inheritance?

// The original inherited root class. For example, the class asstudent inherits from the root class @ interface asstudent: nsobject {}@ end // now inherits from the existing custom class, for example, asstudent inherits from student @ interface asstudent: Student {} @ end

(2) why learning inheritance?

The reason is that there is less code. Something can be reused, that is, something in the parent class. In the Child class, you can directly inherit the parent class without writing code again.


(3) Precautions for inheritance

A: The instance variable of the subclass cannot have the same name as the parent class;

B: The objects initialized by the subclass can call their own functions, or they can call the functions in the parent class, because they are inherited;

C: The instance variable type of the parent class remains unchanged in the subclass. The subclass can only directly access the public and protected instance variables of the parent class. The parent class and the subclass can only be called directly with the public class;

D: The subclass method can overwrite the method of the parent class (if the method name, number of parameters, type, and return value type are the same), The subclass method is generally more detailed, the methods of the parent class are more abstract.

E: initialization rules under multi-level inheritance. One is to re-load Init; the other is to write the specified Initialization Method in your class; the third is the self-class init method, which calls the specified Initialization Method of the class, and the fourth is the specified Initialization Method of the class that calls the specified Initialization Method of the parent class. Otherwise, recursive calls may occur;

F: the message search rule first searches for the class and then searches for the parent class. Note that although class B creates Class B objects, [B display] is used to call the display method, and the display method calls the parent class [Super display] method, but the object is still the class object, so the value is the value of the class initialization.

[OC learning-12] What is class inheritance? Why inherit? And precautions in inheritance

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.