Dark Horse programmer ------ inheritance of OC object-oriented, dark horse ------ oc

Source: Internet
Author: User

Dark Horse programmer ------ inheritance of OC object-oriented, dark horse ------ oc

------ Android training, iOS training, and Java training. We look forward to communicating with you -----


I. Basic concepts of Inheritance

Inheritance is one of the three features of orientation. It refers to defining a new class based on a class. The original class becomes the parent class (base class), and the new class is called the subclass (derived class ).

In life, there are countless examples of inheritance. For example, if a son inherits from his father, then he will enjoy his father's property, not all of which are explained in detail.


Another example is an electrical series:


Next layer, electrical appliances are the parent class. The following washing machines, computers, and televisions are sub-classes of electrical appliances. The brand names below are sub-classes of washing machines, computers, and televisions, and of course electrical appliances.


The benefits of inheritance are as follows:

1) duplicate code is extracted.

2) Relationship between classes


Ii. Inheritance in OC

1) Features

1. subclass has attributes and methods of the parent class;
Second, subclasses can have their own new attributes and methods;
Third, subclass can override the method of the parent class;
4. You can declare the parent class and create a subclass.

2) Benefits

1. improves code reusability. 2. Let the class have a relationship with the class and provide a prerequisite for the third feature polymorphism. Sample Code: Declaration:
/* 1. benefits of inheritance: 1> extracting repeated code 2> establishing the relationship between classes 3> subclass can have all member variables and methods in the parent class 2. note 1> basically, the root class of all classes is NSObject * // ********** Animal declaration *******/@ interface Animal: NSObject {int _ age; double _ weight;}-(void) setAge :( int) age;-(void) setWeight :( double) weight; -(double) weight; @ end

Implementation:
/******** Animal implementation *******/@ implementation Animal-(void) setAge :( int) age {_ age = age ;} -(int) age {return _ age;}-(void) setWeight :( double) weight {_ weight = weight;}-(double) weight {return _ weight ;} @ end/********** Dog ******* //: Animal inherits Animal, it is equivalent to having all the member variables and methods in Animal. // The Animal is called the Dog parent class. // The Dog is called the Animal subclass @ interface Dog: animal @ end @ implementation Dog @ end/********* Cat *******/@ interface Cat: Animal @ end @ implementation Cat @ end

Run the program:
#import <Foundation/Foundation.h>int main(){    Dog *d = [Dog new];        [d setAge:10];        NSLog(@"age=%d", [d age]);    return 0;}


Iii. Notes for inheritance

Note:
1> the parent class must be declared before the subclass.
2> The subclass cannot have the same member variables as the parent class.
3> when a method is called, it is first found in the current class. If not, it is found in the parent class.

When you want to use an inheritance system, 1. view the top-level classes in the system to understand the basic functions of the system. 2. Create the most subclass object in the system to use the function. When should inheritance be defined?When there is a relationship between the class and the class, the inheritance is defined. Xxx is one of yyy. Link: is a link.
Iv. Method RewritingOverride: subclass re-implements a method in the parent class to overwrite the previous practice of the parent class.

Sample Code:

# Import <Foundation/Foundation. h> // Person @ interface Person: NSObject {int _ age;}-(void) setAge :( int) age;-(void) run; + (void) test; @ end @ implementation Person + (void) test {NSLog (@ "Person + test") ;}- (void) run {NSLog (@ "person --- run") ;}- (void) setAge :( int) age {_ age = age ;}- (int) age {return _ age ;} @ end // The member variable with the same name as the subclass and parent class is not allowed. // Student @ interface Student: Person {int _ no; // int _ age;} + (void) test2; @ end @ implementation Student // rewrite: The subclass re-implements a method in the parent class to overwrite the previous method of the parent class-(void) run {NSLog (@ "student --- run") ;}+ (void) test2 {[self test] ;}@ endint main () {[Student test2]; // Student * s = [Student new]; // [s run]; return 0 ;}


V. Use Cases of Inheritance

Inherited application scenarios
1> when two classes have the same attributes and methods, they can be extracted to a parent class.
2> when class A has partial attributes and methods in Class B, you can consider inheriting Class.

A {int _ age; int _ no;} B: A {int _ weight;} // inheritance: xx is xxx // combination: xxx has xxx

Inherit VS combination:

A {     int _age;     int _no; }  B {     A *_a;     int _weight; }



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.