"OC" inheritance, "oc 」

Source: Internet
Author: User

"OC" inheritance, "oc 」
I. Basic usage1. design two types of Bird and Dog

1 // Bird Declaration 2 @ interface Bird: NSObject 3 {4 @ public 5 int weight; 6} 7-(void) eat; 8 @ end 9 // definition of Bird 10 @ implementation Bird11-(void) eat {12 NSLog (@ "eat-weight: % d", weight ); 13} 14 @ end15 // The Declaration of Dog 16 @ interface Dog: NSObject17 {18 @ public19 int weight; 20} 21-(void) eat; 22 @ end23 // Dog definition 24 @ implementation Dog25-(void) eat {26 NSLog (@ "eat-weight: % d", weight); 27} 28 @ end
2. With the same attributes and behaviors, extract a parent class Animal (the weight attribute is extracted first, and then the eat method is extracted)
1 // Animal Declaration 2 @ interface Animal: NSObject 3 {4 @ public 5 int weight; 6} 7-(void) eat; 8 @ end 9 // Animal definition 10 @ implementation Animal11-(void) eat {12 NSLog (@ "eat-weight: % d", weight ); 13} 14 @ end
3. Add attributes and methods to sub-classes on the basis of parent classes
1 // Bird Declaration 2 @ interface Bird: Animal 3 {4 @ public 5 int height; 6} 7-(void) fly; 8 @ end 9 10 // definition of Bird 11 @ implementation Bird12-(void) fly {13 NSLog (@ "-height: % d", height ); 14} 15 @ end16 17 // Dog statement 18 @ interface Dog: Animal19 {20 @ public21 int speed; 22} 23-(void) run; 24 @ end25 // Dog definition 26 @ implementation Dog27-(void) run {28 NSLog (@ "Running-Height: % d", speed); 29} 30 @ end
Ii. 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;

(4) The OC language is a single inheritance language. In the OC language, basically the root classes of all classes are NSObject classes.

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

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.

III,SuperKeywords

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.

Iv. benefits and disadvantages of Inheritance

Benefits of inheritance:

(1) extracted repeated code

(2) establish a connection between classes

Disadvantages of inheritance:

Strong Coupling

V. 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.

 

Related Article

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.