Inheritance of Three Object-Oriented Characteristics

Keywords: C# Programming

What is inheritance?

Inheritance is to extract the same attributes and methods from the two classes and place them in a new class (parent class), and the two subclasses still have these attributes and methods. Abstract point

Every car can run (with the method of running) and has a tyre (with the property of tyre).

 

The benefits of using inheritance programming?

Inheritance programming can greatly reduce code redundancy and increase code reuse.

 

How to use inheritance?

Take the first question as an example to extend: if we need two categories now, one for trucks and one for cars. The code is as follows

/// <summary>
 2     /// Truck class
 3     /// </summary>
 4     class Truck
 5     {
 6         //Speed per hour
 7         public int Speed { get; set; }
 8         //colour
 9         public string Colour { get; set; }
10         //Purchase price
11         public double Price { get; set; }
12         
13         /// <summary>
14         /// Freight transportation method
15         /// </summary>
16         public void Cargo()
17         {
18             Console.WriteLine("freight");
19         }
20     }
21 
22     /// <summary>
23     /// Car class
24     /// </summary>
25     class Sedan
26     {
27         //Speed per hour
28         public int Speed { get; set; }
29         //colour
30         public string Colour { get; set; }
31         //Purchase price
32         public double Price { get; set; }
33         
34         /// <summary>
35         /// Passenger carrying method
36         /// </summary>
37         public void CarryPassengers()
38         {
39             Console.WriteLine("Carry passenger");
40         }
41     }
Example: Unused inheritance

 

Through this code, it is not difficult to find that trucks and cars have three identical attributes and a different approach. According to inheritance characteristics, the same attributes or methods need to be extracted

Take it out. The code is as follows

/// <summary>
 2     /// Vehicle parent
 3     /// </summary>
 4     class Vehicle
 5     {
 6         //Speed per hour
 7         public int Speed { get; set; }
 8         //colour
 9         public string Colour { get; set; }
10         //Purchase price
11         public double Price { get; set; }
12     }
13 
14     /// <summary>
15     /// Truck Class Inheritance Vehicle Class
16     /// </summary>
17     class Truck : Vehicle
18     {
19         /// <summary>
20         /// Freight transportation method
21         /// </summary>
22         public void Cargo()
23         {
24             Console.WriteLine("freight");
25         }
26     }
27 
28     /// <summary>
29     /// Car Class Inheritance Vehicle Class
30     /// </summary>
31     class Sedan : Vehicle
32     {
33         /// <summary>
34         /// Passenger carrying method
35         /// </summary>
36         public void CarryPassengers()
37         {
38             Console.WriteLine("Carry passenger");
39         }
40     }
Example: Using inheritance

     

The question arises, then, where does inheritance feature-code reuse manifest itself? Let's take a brief look at it.

In the first section of the code, there are two classes, and if we want to initialize properties using parametric constructors when creating objects, we need to write the same generation in both classes.

Code. ( Constructor Explanation)

 1     /// <summary>
 2     /// Truck class
 3     /// </summary>
 4     class Truck
 5     {
 6         //Speed per hour
 7         public int Speed { get; set; }
 8         //colour
 9         public string Colour { get; set; }
10         //Purchase price
11         public double Price { get; set; }
12         public Truck(int speed, string colour, double price)
13         {
14             this.Speed = speed;
15             this.Colour = colour;
16             this.Price = price;
17         }
18         ...
19     }
20 
21     /// <summary>
22     /// Car class
23     /// </summary>
24     class Sedan
25     {
26         //Speed per hour
27         public int Speed { get; set; }
28         //colour
29         public string Colour { get; set; }
30         //Purchase price
31         public double Price { get; set; }
32         public Truck(int speed, string colour, double price)
33         {
34             ...
35         }
36         ...
37     }
Example: Redundant code

 

Obviously, there is a lot of duplicate code above. Now let's use inheritance to simplify the above code.

 1     /// <summary>
 2     /// Vehicle parent
 3     /// </summary>
 4     class Vehicle
 5     {
 6         //Speed per hour
 7         public int Speed { get; set; }
 8         //colour
 9         public string Colour { get; set; }
10         //Purchase price
11         public double Price { get; set; }
12 
13         /// <summary>
14         /// Parametric constructor
15         /// </summary>
16         /// <param name="speed"></param>
17         /// <param name="colour"></param>
18         /// <param name="price"></param>
19         public Vehicle(int speed, string colour, double price)
20         {
21             this.Speed = speed;
22             this.Colour = colour;
23             this.Price = price;
24         }
25     }
26 
27     /// <summary>
28     /// Truck Class Inheritance Vehicle Class
29     /// </summary>
30     class Truck : Vehicle
31     {
32         /// <summary>
33         /// Subclasses use parent constructors
34         /// </summary>
35         public Truck(int speed, string colour, double price) :
36             base (speed,colour,price)
37         {
38 
39         }
40         ...
41     }
42 
43     /// <summary>
44     /// Car Class Inheritance Vehicle Class
45     /// </summary>
46     class Sedan : Vehicle
47     {
48         /// <summary>
49         /// Subclasses use parent constructors
50         /// </summary>
51         public Sedan(int speed, string colour, double price) :
52             base (speed,colour,price)
53         {
54 
55         }
56         ...
57     }
Example: Simplified code

 

After using inheritance, even if more derived classes need parameterized constructors, they are not afraid to do so, as long as one line of code.

Base keyword:

The base keyword represents the parent class and can be used to access members of the parent class. Example: base. parent class member.

 

In addition to calling the parent parametric constructor shown above through base, the parent parametric constructor is implicitly called when creating the child class object. (When creating subclass objects

The parametric constructor of the parent class is called first.

 

Further understanding and use of inheritance

The characteristics of inheritance:

1. Transitivity: Inheritance can be derived infinitely downward, but one condition needs to be met: subclasses can appear where the parent class appears and replace the parent class, that is, subclasses.

Have all the features of the parent class and all the methods to implement the parent class.

2. Single root: A subclass can only inherit from a parent class. There is no case where a subclass inherits from two or more parent classes at the same time.

is a keyword:

This keyword is used to determine whether an object belongs to a given type. The return value is bool type.

Example:

            if (Vehicle is Truck)

            {
                ...
            }

sealed keyword:

Classes modified with this keyword cannot be inherited, that is, sealed classes. The commonly used string class is the sealed class. This article only explains, and then gives an example.

 

So far, I've talked about inheritance. Here's all the code used in this article. I hope I can help you see here.

  1     class Program
  2     {
  3         static void Main(string[] args)
  4         {
  5             Demo demo = new Demo();
  6             demo.Test();
  7 
  8             Console.Read();
  9         }
 10     }
 11 
 12     class Demo
 13     {
 14         List<Vehicle> vehicles = new List<Vehicle>();
 15         
 16         public void Test()
 17         {
 18             vehicles.Add(new Truck(50, "blue", 150000));
 19             vehicles.Add(new Sedan(80, "black", 210000));
 20             foreach (Vehicle item in vehicles)
 21             {
 22                 if (item is Truck)
 23                 {
 24                     Console.WriteLine("I'm a truck. Speed{0},I am{1}Price.{2}Yuan.", item.Speed, item.Colour, item.Price);
 25                     ((Truck)item).Cargo();
 26                 }
 27                 if (item is Sedan)
 28                 {
 29                     Console.WriteLine("I'm a car. Speed per hour.{0},I am{1}Price.{2}Yuan.",item.Speed,item.Colour,item.Price);
 30                     ((Sedan)item).CarryPassengers();
 31                 }
 32             }
 33         }
 34     }
 35 
 36     /// <summary>
 37     /// Vehicle parent
 38     /// </summary>
 39     class Vehicle
 40     {
 41         //Speed per hour
 42         public int Speed { get; set; }
 43         //colour
 44         public string Colour { get; set; }
 45         //Purchase price
 46         public double Price { get; set; }
 47 
 48         /// <summary>
 49         /// Parametric constructor
 50         /// </summary>
 51         /// <param name="speed"></param>
 52         /// <param name="colour"></param>
 53         /// <param name="price"></param>
 54         public Vehicle(int speed, string colour, double price)
 55         {
 56             this.Speed = speed;
 57             this.Colour = colour;
 58             this.Price = price;
 59         }
 60     }
 61 
 62     /// <summary>
 63     /// Truck Class Inheritance Vehicle Class
 64     /// </summary>
 65     class Truck : Vehicle
 66     {
 67         /// <summary>
 68         /// Subclasses use parent constructors
 69         /// </summary>
 70         public Truck(int speed, string colour, double price) :
 71             base (speed,colour,price)
 72         {
 73 
 74         }
 75 
 76         /// <summary>
 77         /// Freight transportation method
 78         /// </summary>
 79         public void Cargo()
 80         {
 81             Console.WriteLine("I can transport goods.");
 82         }
 83     }
 84 
 85     /// <summary>
 86     /// Car Class Inheritance Vehicle Class
 87     /// </summary>
 88     class Sedan : Vehicle
 89     {
 90         /// <summary>
 91         /// Subclasses use parent constructors
 92         /// </summary>
 93         public Sedan(int speed, string colour, double price) :
 94             base (speed,colour,price)
 95         {
 96 
 97         }
 98         /// <summary>
 99         /// Passenger carrying method
100         /// </summary>
101         public void CarryPassengers()
102         {
103             Console.WriteLine("I can carry passengers.");
104         }
105     }
Complete code

 

    

 

 

 

Concluding remarks: It is meaningless to learn knowledge without sharing it with others.

Posted by HaXoRL33T on Tue, 02 Apr 2019 18:54:31 -0700