[Learning notes] [OC language] inheritance, learning notes oc inheritance

Source: Internet
Author: User

[Learning notes] [OC language] inheritance, learning notes oc inheritance

1. Basic inheritance usage

1 # import <Foundation/Foundation. h> 2/* 3 1. benefits of inheritance: 4 1> extracting repeated code 5 2> establishing a relationship between classes 6> subclass can have all member variables and methods in the parent class 7 8 2. note 9 1> basically, the root class of all classes is NSObject10 */11 12 13/********* Animal declaration *******/14 @ interface Animal: NSObject15 {16 int _ age; 17 double _ weight; 18} 19 20-(void) setAge :( int) age; 21-(int) age; 22 23-(void) setWeight :( double) weight; 24-(double) weight; 25 @ end26 27/********* implementation of Animal *******/28 @ implementation Animal29-(void) setAge :( int) age30 {31 _ age = age; 32} 33-(int) age34 {35 return _ age; 36} 37 38-(void) setWeight :( double) weight39 {40 _ weight = weight; 41} 42-(double) weight43 {44 return _ weight; 45} 46 @ end47 48/********** Dog *******/49 //: Animal inherits Animal, equivalent to having all member variables and methods in Animal 50 // Animal is called Dog's parent class 51 // Dog is called Animal's subclass 52 @ interface Dog: animal53 @ end54 55 @ implementation Dog56 @ end57 58/********* Cat *******/59 @ interface Cat: animal60 @ end61 62 @ implementation Cat63 @ end64 65 int main () 66 {67 Dog * d = [Dog new]; 68 69 [d setAge: 10]; 70 71 NSLog (@ "age = % d", [d age]); 72 return 0; 73}

Subclass method and attribute access process: If the subclass does not exist, access
If the parent class is inherited, it can still be used as usual.
Static Method of parent class
Draw an inheritance structure diagram to extract child classes to parent classes
NSObject Introduction: The final parent class of all OC classes, including some common methods, such as + new

2. inherited terminology
Parent class \ superclass
Subclass \ subclasses

3. Inheritance details
Single inheritance
The subclass and parent classes cannot have the same member variables.
Method Rewriting

4. super keyword
Call the object methods and class methods of the parent class respectively.

1/* 2 botnets 3 4 jumping botnets, dancing kings botnets, and bucket botnets 5 */6 # import <Foundation/Foundation. h> 7 8/* 9 super effect 10 1. directly call a method in the parent class 11. super is in the object method, then it will call the object method of the parent class 12 super is in the class method, then it will call the class method of the parent class 13 14 3. usage: When the subclass overrides the parent class method, it wants to retain some behavior of the parent class 15 */16 17 // zombie 18 @ interface Zoombie: NSObject19-(void) walk; 20 21 + (void) test; 22-(void) test; 23 24 @ end25 26 @ implementation Zoombie27-(void) listen 28 {29 NSLog (@ "move two steps forward ******"); 30} 31 32 + (void) test33 {34 NSLog (@ "Zoombie + test"); 35} 36 37-(void) test38 {39 NSLog (@ "Zoombie-test "); 40} 41 @ end42 43 // jump zombie 44 @ interface JumpZoombie: Zoombie45 + (void) haha; 46-(void) haha2; 47 @ end48 49 50 @ implementation JumpZoombie51 52 + (void) hahaha53 {54 [super test]; 55} 56 57-(void) haha258 {59 [super test]; 60} 61 62-(void) Listen 63 {64 // skip two times 65 NSLog (@ "skip two times"); 66 67 // take two times (directly call the walk Method of the parent class) 68 [super walk]; 69 // NSLog (@ "move two steps forward ----"); 70 71} 72 @ end73 74 int main () 75 {76 // [JumpZoombie haha]; 77 JumpZoombie * jz = [JumpZoombie new]; 78 79 [jz haha2]; 80 81 return 0; 82}

5. Benefits of Inheritance
Without changing the original model, the Extension Method
Establish a connection between classes
Extracted public code
Disadvantage: Strong Coupling

6. Use Cases of Inheritance
All its attributes are what you want. Generally, they are inherited.
Some of its attributes are what you want. You can extract another parent class.

7. Code

1/* 2 1. override: subclass re-implements a method in the parent class, covering the previous practice of the parent class 3 2. note 4 1> the parent class must be declared before the subclass. 5 2> The subclass cannot have the same member variable as the parent class. 6 3> when calling a method, It takes precedence to find it in the current class, if not, go to the parent class to find 7 8 2. disadvantage: the coupling is too strong 9 */10 11 # import <Foundation/Foundation. h> 12 // Person13 @ interface Person: NSObject14 {15 int _ age; 16} 17 18-(void) setAge :( int) age; 19-(int) age; 20 21-(void) run; 22 23 + (void) test; 24 25 @ end26 27 @ implementation Person28 29 + (void) test30 {31 NSLog (@ "Person + test"); 32} 33 34-(void) run35 {36 NSLog (@ "person --- Run "); 37} 38 39-(void) setAge :( int) age40 {41 _ age = age; 42} 43-(int) age44 {45 return _ age; 46} 47 @ end48 49 // The member variable with the same name as the parent class is not allowed. 50 // Student51 @ interface Student: Person52 {53 int _ no; 54 // int _ age; 55} 56 57 + (void) test2; 58 59 @ end60 61 @ implementation Student62 // rewrite: subclass re-implements a method in the parent class, overwrite the previous practice of the parent class 63-(void) run64 {65 NSLog (@ "student --- Run"); 66} 67 68 + (void) test269 {70 [self test]; 71} 72 @ end73 74 75 int main () 76 {77 [Student test2]; 78 79 // Student * s = [Student new]; 80 // 81 // [s run]; 82 83 return 0; 84}

Inherited application scenarios

1/* 2 1. inheritance usage 3 1> when two classes have the same attributes and methods, you can extract the same things to A parent class. 4 2> when class A has partial attributes and methods in Class B, class B can be considered to inherit Class A 5 A 6 {7 int _ age; 8 int _ no; 9} 10 11 B: A12 {13 int _ weight; 14} 15 16 // inheritance: xx is xxx17 // combination: xxx has xxx18 19 2. combination of 20 A21 {22 int _ age; 23 int _ no; 24} 25 26 B27 {28 A * _ a; 29 int _ weight; 30} 31 */32 33 @ interface Score: NSObject34 {35 int _ cScore; 36 int _ ocScore; 37} 38 @ end39 40 @ implementation Score41 @ end42 43 @ interface Student: NSObject44 {45 // combination 46 Score * _ score; 47 // int _ cScore; 48 // int _ ocScore; 49 int _ age; 50} 51 @ end52 53 @ implementation Student54 55 @ end

 

 

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.