OC object-oriented-Inheritance

Source: Internet
Author: User

I. Basic Concepts

There is no difference in thinking between the world of programs and the "object" World of mankind. The rich generation inherits from its parents and naturally owns all the resources of its parents, subclass inherits the parent class and has all the methods and attributes (member variables) of the parent class ).

Here, animals are the parent class of cats and dogs, and black and white cats are the child classes of cats.

Benefits of inheritance:

(1) extracted repeated code

(2) establish a connection between classes

Disadvantages of inheritance:

Strong Coupling

 

Ii. Inheritance in oC

@ Interface animal: nsobject

// An animal inherits nsobject and obtains nsobject class methods;

@ End

 

@ Interface dog: Animal

// The Dog class inherits the animal class

@ End

Note: The OC language is a single inheritance language. In the OC language, basically the root classes of all classes are nsobject classes.

 

Iii. Use of Inheritance

(1) The Compiler executes the statements from top to bottom, so at least the declaration of the parent class should exist before the subclass;

(2) The member variable names with the same name in the subclass and parent classes are not allowed in OC;

(3) The subclass in OC can have methods with the same name as the parent class. When the subclass is called, it is preferred to search internally. If there is no subclass, it will be searched up one layer at a time;

Tip: rewrite means that the subclass re-implements a method in the parent class, overwriting the previous implementation of the parent class.

: There are three classes in total. The person class inherits the nsobject class, and the student class inherits the person class.

Create a student * s = [[STUDENT alloc] init];

In this case, the student class and the parent class of this class are loaded into the memory.

Tip: each class has a super class pointer pointing to its parent class. The object has an ISA pointer pointing to the class that calls the object.

 

Iv. Inheritance and combination

Applicable scenarios of inheritance:

(1) When two classes have the same attributes and methods, the same attributes and methods can be extracted to a parent class.

(2) When Class A has partial attributes and methods in Class B, Class B can be considered to inherit Class A (consideration). In this case, you can also consider using combinations.

Inheritance: ### it is XXX. For example, if a dog is an animal, the dog can inherit the animal class.

Combination: ### possess XXX. If a student has a book, the book class can be used as an attribute of the student class.

 

5. Keyword super

Super keyword. When you override a method in a subclass, the caller can skip this layer and call the method in the parent class.

Purpose:

(1) directly call a method in the parent class

(2) If super is in the object method, the object method of the parent class will be called; if super is in the class method, the class method of the parent class will be called.

Usage scenario: When a subclass overrides the parent class method, it wants to retain some behavior of the parent class.

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.