Dark Horse programmer 06-Inheritance

Source: Internet
Author: User

--- Java training, Android training, IOS training, and. Net training. We look forward to communicating with you! ---

1. Concept of Inheritance

Dog: Attributes of age and weight, and running behavior

Cat: Attributes of age and weight, and running behavior

The two have the same attributes and behaviors. During code writing, duplicate code appears, affecting the efficiency.

Therefore, we extract the commonalities of two classes and define an animal.

The dog and cat classes inherit the animal class. dog and cat are called animal subclasses, and animal is called the parent classes of dog and cat.

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}

2. Benefits of Inheritance

(1) duplicate code extraction

(2) establish the relationship between classes

(3) subclass can have all member variables and methods in the parent class

3. Notes

(1) Basically, the base class of all classes is nsobject.

(2) The parent class must be written before the subclass.

(3) member variables with the same name in the subclass and parent classes are not allowed.

(4) When a method is called, it is first found in the current object. If it cannot be found, it is found in the parent class.

4. Disadvantages

The coupling is too strong, and the relationship between classes is too close.

 

Dark Horse programmer 06-Inheritance

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.