oc--Inheritance of iOS development

Source: Internet
Author: User

Three main features of object-oriented

1) Encapsulation (set method and get method, Master)

2) Inheritance (Mastery)

3) Polymorphism (understanding concept can be)

(a) Inheritance (a syntax): is used between classes and classes, representing the relationship between (two) classes.

1. Application of Inheritance

You want to get all the member variables and methods of a class, and what you want to achieve is inherited.

1> when two classes have the same properties and methods, you can extract the same things into a parent class

2> when Class A fully owns some of the properties and methods in class B, consider letting class B inherit class a

Benefits of inheritance:

1> extract Duplicate code, increase code reuse rate

2> establishes a relationship between classes

3> subclasses can have all member variables and methods in the parent class

Disadvantages of inheritance:

Strong coupling of code (that is, class-to-class relationships)

> Note points: basically, the root class of all classes is NSObject

2.

key points of succession:

1) When invoking a method, first detects whether the subclass implements this method, and if the subclass is not implemented, it invokes the implementation of the parent class

2) Rewrite: Subclasses implement methods declared in the parent class

3) A member variable declared inside a subclass cannot have the same name as a member variable declared inside a parent class

code example:

/* * /* 1. Rewrite: Subclasses re-implements a method in the parent class, overwriting the previous practice of the parent Class 2. Note that the 1> parent class must be declared before the child class 2> Subclasses cannot have the same member variable as the parent class 3> call a method, take precedence in the current class to find, if not found, go to the  parent class * *#import <foundation/ foundation.h>@interface person:nsobject     //   Colon represents an inheritance relationship followed by the parent class @end 

(ii) Super

the role of super

1) Call a method in the parent class directly.

2) Super is in the object method, then the parent class's object method is called.

Super is in a class method, then the class method of the parent class is called.

3) Usage Scenarios: Subclasses override the parent class's methods to preserve some behavior of the parent class.

Thought: For some of the same attributes, a common class is generally extracted.

oc--Inheritance of iOS development

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.