[.net Basics of Object-Oriented Programming] (11) Three major characteristics of object-oriented-inheritance

This article is transferred from: https://www.cnblogs.com/yubinfeng/p/4555948.html

     In the previous section, we talked about the encapsulation of one of the three major object-oriented features, which solves the problem of putting all the information that can be operated on the same object together, implementing unified external calls, achieving the reuse of the same object, and reducing coupling.

     However, in practical applications, many objects have the same or similar attributes. For example, there is an object fruit tree (FruitTree), which has a member attribute leaf (Leaf), will bloom (Flower), has a stem (Stem), has a tree root ( Root), it will grow (Growth method).

     There is another object apple tree (AppleTree) which is also a fruit tree, with all the characteristics of fruit trees, then we defined an apple tree object when programming, if there is another orange tree (OrangeTree), peach tree (PeachTree), we Can't you always copy this object and change its name? Here we will use the second object-oriented feature: inheritance.

1. What is inheritance

     There is an "is-a" relationship between FruitTree and OrangeTree above, that is, we can say "orange tree is a fruit tree". In object-oriented programming, we call this relationship "inheritance", that is, the orange tree inherits from the fruit tree. In other words, the orange tree is a derivative of the fruit tree; it can also be said that the fruit tree is the parent class, and the orange tree is the child class. The same apple tree can also inherit fruit trees, so apple trees can also be said to be a subclass of fruit trees. Here we find that a class can have multiple derived classes, that is, a class can be inherited by multiple classes .

Through the above example, we summarize the concepts related to inheritance:

(1) When a class A can obtain the definition of all non-private data and operations in another class B as part or all of its own components, it is said that there is an inheritance relationship between the two classes.

(2) The inherited class B is called the parent class or base class, and the class A that inherits the parent class is called the child class or derived class .

2. Inherited characteristics

In the above example, if the apple tree inherits from the fruit tree, then the apple tree has all the attributes (leaves, roots, flowers) and methods (growth) of the fruit tree. The apple tree also has some unique attributes, such as its own fruit. Apple (Apple); the same peach tree has its own fruit peach (Peach), so inherited subclasses can have their own unique members (properties or methods, etc.).

Feature one: In addition to inheriting the characteristics of the parent class, derived classes can also have their own unique characteristics

In addition to the public members such as leaves, roots, and flowers, the FruitTree mentioned above can also have its own private members, such as species (deciduous fruit trees, evergreen fruit trees), and the "species" member, It is not owned by its subclasses AppleTree and OrangeTree, so it is a private member. After the subclass inherits the parent class, it cannot have the private members of the parent class.

Feature two: Subclasses cannot have private members of the parent class

The above example, if the tree has a public method of growth (Growth), it has two subclasses peach tree and apple tree, then the subclass also has the growth method, but the peach tree and apple tree growth process is different We can modify this method to adapt to the growth of different kinds of fruit trees.

Feature three: Subclasses can implement the functions of the parent class in their own way (that is, method rewriting, which is specifically introduced later)

3 . Inherited implementation

     Through the above example, we are already familiar with inheritance, leaving aside the concept. Simply put, the word inheritance comes from life, there is property inheritance, spiritual inheritance. Object-oriented programming is nothing more than abstraction of these concepts, in a layman's terms, "apple tree is a fruit tree"   

The code implements the above example   

Copy code
  1 /// <summary> 
  2 /// Fruit tree class 
  3 /// </ summary> 
  4 class FruitTree 
  5 { 
  6 /// <summary> 
  7 /// Name 
  8 /// Description: The modifier protects access. Access is limited to this class and subclasses, and instances cannot be accessed. 
  9 /// </ summary> 
 10 protected string name; 
 11 /// <summary> 
 12 /// constructor 
 13 /// </ summary> 
 14 public FruitTree () 
 15 { 
 16 this.name = "no name"; 
 17} 
 18 /// <summary> 
 19 /// Constructor 2 
 20 /// </ summary> 
 21 /// <param name = "name"> <
 24 this.name = name; 
 25} 
 26 object _leaf; 
 27 object _root; 
 28 object _flower; 
 29 string _type; 
 30 /// <summary> 
 31 /// leaf (public property) 
 32 /// </ summary> 
 33 public object leaf 
 34 { 
 35 get {return _leaf;} 
 36 set {_leaf = value;} 
 37} 
 38 /// <summary> 
 39 /// root (public property) 
 40 /// </ summary> 
 41 public object root 
 42 { 
 43 get {return _root;}
 44 set {_root = value;} 
 45} 
 46 /// <summary> 
 66 /// Apple tree
 47 /// Flower (public property) 
 48 /// </ summary> 
 49 public object flower 
 50 { 
 51 get {return _flower;} 
 52 set {_flower = value;} 
 53} 
 54 /// <summary> 
 55 // / Category (no modifier defined, default is private) 
 56 /// </ summary> 
 57 string type 
 58 { 
 59 get {return _type;} 
 60 set {_type = value;} 
 61} 
 62    
 63} 
 64 
 65 /// <summary> 
 67 /// Inherited from: Fruit Tree 
 68 /// </ summary> 
 69 class AppleTree: FruitTree 
 70 {
 71 string _myName; 
 72 /// <summary> 
 73 /// Constructor 
 74 /// Description: The subclass calls the same constructor of the parent class, you need to use: base () 
 75 /// </ summary> 
 76 public AppleTree (): base () 
 77 {           
 78} 
 79 /// <summary> 
 80 /// Constructor 2 
 81 /// Description: Subclasses call the same constructor of the parent class, you need to use: base (name) 
 82 / // </ summary> 
 83 /// <param name = "name"> </ param> 
 84 public AppleTree (string name): base (name) 
 85 { 
 86 _myName = name; 
 87}             
 90 /// returns the name of the fruit 
 91 /// </ summary> 
 88
 89 /// <summary> 
 92 /// <returns> </ returns> 
 93 public string MyFruitName () 
 94 { 
 95 return "I am:" + _myName + "; my fruit is called: Apple"; 
 96} 
 97} 
 98 /// <summary> 
 99 /// 
Orange 
tree class 100 /// Inherited from: Fruit tree class 
101 /// </ summary> 102 class OrangeTree: FruitTree 
103 { 
104 string _myName; 
105 /// <summary> 
106 /// Constructor 
107 /// Description: Subclasses call the same constructor of the parent class, you need to use: base () 
108 /// </ summary> 
109 public OrangeTree ():base()
110         {
111         }
112         /// <summary>
113 /// Constructor 2  
114 /// Description : The subclass calls the same constructor of the parent class, you need to use:base(name)
115         /// </summary>
116 /// <param name = "name"> </ param> 
117 public OrangeTree (string name): base (name) 
118 { 
119 _myName = name; 
120} 
121 
122 /// <summary> 
123 /// returns the name of the fruit 
124 /// </ summary> 
125 /// <returns> </ returns> 
126 public string MyFruitName () 
127 { 
128 return "I am:" + _myName + "; I The fruit is called: orange "; 
129} 
130}
Copy code

Call the subclass:

// Call the subclass 
AppleTree appleTree = new AppleTree ("Apple Tree"); 
string myName = appleTree.MyFruitName (); 
// The return result is: I am: apple tree; my fruit is called: apple

 

// Call subclass 
OrangeTree orangeTree = new OrangeTree ("橙 树");
string myName = orangeTree. MyFruitName ();
// The returned result is: I am: orange tree; my fruit is called: orange

      Through this code, we can see that there is a base class fruit tree, then we have hundreds of trees, only need one inheritance, for the subclass AppleTree.MyFruitName () returns the name of this method, in different subclass Can be unique, that is, the characteristics of inheritance, can add unique members. Although the unique characteristics need to be defined separately in each subclass, sharing the parent class members has saved us a lot of work, and the structure of the most important program is clearer and easier to maintain.

 

4. Disadvantages of inheritance

     Seeing this title, the friends may be surprised, since there are so many advantages of object-oriented inheritance, there are still disadvantages. Of course, there is nothing perfect in the world, and so is inheritance.

     Disadvantage one: the parent class changes, the child class has to change;

     Disadvantage two: inheritance breaks the packaging, and the details of the parent class are exposed to the child class.

     The previous section said that the independent feature of encapsulation is to reduce the coupling, and inheriting it in order to achieve reuse has increased the coupling.

     Speaking of where the friends are entangled, then whether to use inheritance or not, the answer is yes, its advantages and light cover up the shortcomings, that is to say, more benefits. Here to explain its shortcomings, is to remind us to avoid the consequences of its shortcomings as much as possible in the use process.

     So how can we use inheritance well? We should pay attention to these points:

a. When the relationship between the two objects is "is a", you can use inheritance (such as the apple tree is a tree); b. When the two objects are "as a" relationship, it is not appropriate to use inheritance (such as the hand is part of the human , Can't let the hand heir);

     With regard to the advantages and disadvantages of inheritance, we should remember one thing: to use inheritance in a reasonable way, we can achieve the best results, not blindly.

 

     As one of the three major features of object-oriented: inheritance, it can be said that it is the top priority of learning object-oriented programming. Because this section can be said to be the focus of this series, there is no one.

    My friends, it is early morning again and I will continue to write tomorrow. Finally, according to the convention, write a few points that need to be paid attention to when using inheritance.

 

Key points:

1: Private members in the parent class, derived classes are never accessible;

2: C # requires that a class can only have one direct base class;

3: Classes modified by the "sealed" keyword cannot be inherited;

4: Members or data modified by "protected" can be directly accessed by derived classes and belong to "secrets that can be shared in the family".

5: Make good use of the "base" keyword to show that the appropriate custom base class constructor is called instead of using the default constructor.

6: Inheritance requires reasonable use in order to achieve the best results. In general, it applies to the "is a" relationship, not the "has a" relationship.

Guess you like

Origin www.cnblogs.com/hanguoshun/p/12728375.html