Summary of instantiation order and method rewriting usage in class inheritance and polymorphism

Time:2021-2-4

This paper summarizes the instantiation order when using class inheritance and polymorphism, as well as the usage and rules of new, virtual, abstract and override in method inheritance rewriting

Inheritance relation: when instantiating an object, the scope of action type is from the initial base class to the class of the specific instance object (including the subclass of the parent class, member access rights: private, protected, public)

1. The subclass constructor must specify the constructor with the access permission of the parent class: base (xxx), which has no parameters by default;

2. First instantiate the base class, then instantiate the subclass, then the subclass of the subclass… Finally to the class to be instantiated (the constructor execution order is from parent to child…);

3. When you run a method, you run the method in the current type:

1) When a new method is hidden (in fact, signature hiding is used in C #, as long as the method name and parameters are the same, it will be hidden, but new just eliminates the compiler warning), it means that the subclass method is brand new and does not have the rewriting relationship with the parent class, and still runs the method in the current type;

2) When virtual virtual, abstract, and inherit override methods are encountered, it means that the specific implementation of the method has been rewritten, and the rewriting method of the class closest to the specific object type relationship will be run. Of course, the closest thing to you is yourself.

Finally, an example is given

Here’s to mention: an abstract class cannot be instantiated directly, that is, it cannot explicitly call the constructor (new, reflection). When it needs to be instantiated with the help of a subclass, the constructor calls the constructor of the abstract class. For example, base() is instantiated for inheritance and rewriting. Abstract instance members can only be placed in the abstract class. Static classes can only have static members (including constructors) and cannot be instantiated When the constructor is called explicitly, the system automatically creates a static object through the constructor during initial loading.

Summary of instantiation order and method rewriting usage in class inheritance and polymorphismSummary of instantiation order and method rewriting usage in class inheritance and polymorphism

1     public abstract class A
 2     {
 3         public int x;
 4         public A()
 5         {
 6             x = -1;
 7             Console.WriteLine("x={0}", x);
 8             Print1();
 9             Print2();
10         }
11         public virtual void Print1() 
12         {
13             x = 1;
14             Console.WriteLine("x={0}", x);
15         }
16 
17         public virtual void Print2() 
18         {
19             x = 2;
20             Console.WriteLine("x={0}", x);
21         }
22     }
23 
24     public class B : A
25     {
26         public int y;
27         public B()
28         {
29             y = -1;
30             Console.WriteLine("x={0},y={1}", x, y);
31             Print1();
32             Print2();
33         }
34 
35         public override void Print1()
36         {
37             x = 3;
38             y = 1;
39             Console.WriteLine("x={0},y={1}", x, y);
40         }
41 // this method recreates the inherited, and it is the same method itself and its subsequent subclasses
42         public new virtual void Print2()
43         {
44             x = 4;
45             y = 2;
46             Console.WriteLine("x={0},y={1}", x, y);
47         }
48     }
49     public class C : B
50     {
51 // this method recreates the inherited method, which is inherited by itself and its subsequent subclasses
52         public new void Print1()
53         {
54             x = 5;
55             y = 3;
56             Console.WriteLine("x={0},y={1}", x, y);
57         }
58 // this method is overridden from the parent class B instead of the base class A
59         public override void Print2()
60         {
61             x = 4;
62             y = 2;
63             Console.WriteLine("x={0},y={1}", x, y);
64         }
65     }
66     public static class D
67     {
68         public static string _name;
69         static  D()
70         {
71             _ Name = Xiaoming;
72         }
73     }

View Code

Main program:

Summary of instantiation order and method rewriting usage in class inheritance and polymorphismSummary of instantiation order and method rewriting usage in class inheritance and polymorphism

1         static void Main(string[] args)
 2         {
 3             A c = new C();
 4             c.Print1();
 5             c.Print2();
 6 // static and abstract class reflection cannot be created
 7             //Type type = typeof(A);
 8             //var constructor = type.GetConstructor(Type.EmptyTypes);
 9             //Object obj = constructor.Invoke(null);
10             //Console.WriteLine(obj.GetType().Name);
11             //Type type = typeof(D);
12             //var constructor = type.GetConstructor(BindingFlags.Static|BindingFlags.NonPublic,null,Type.EmptyTypes,null);
13             //Object obj = constructor.Invoke(null);
14             //Console.WriteLine(obj.GetType().Name);
15             Console.WriteLine(D._name);
16             Console.ReadKey();
17         }
18

View Code

Output results:

Recommended Today

Practice analysis of rust built-in trait: partialeq and EQ

Abstract:Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. This article is shared from Huawei cloud community《Analysis of rust built-in trait: partialeq and EQ》Author: debugzhang Rust uses traits in many places, from simple operator overloading to subtle features like send and sync. Some traits can be automatically […]