In object-oriented programming, inheritance and polymorphism is an important mechanism, before I share my understanding of "class inheritance", which I share with you today, my understanding of polymorphism (C #).
First, let's look at the polymorphic definition, the same operation acting on different objects, can have different interpretations, and produce different execution results.
There are two forms in C #, one is the polymorphic of compiling, one is the polymorphic of operation, and the multi-state is overloaded, overridden, virtual, abstract and hidden.
Here's a look at this piece of code, and we'll analyze their concepts and differences from the code:
Public abstract class A
{
public abstract void Prind ();
}
public class B:a
{
public override void Prind ()
{
Console.WriteLine ("I am class B, I rewrote the abstract method of Class A Prind ()");
}
public virtual void Bprind ()
{
Console.WriteLine ("I am a virtual method in class B, I can be overridden by a derived class that inherits Class B");
}
public void Nprind ()
{
Console.WriteLine ("Nprind");
}
}
public class C:b
{
public override Bprind ()
{
Console.WriteLine ("I rewrote the virtual method of Class B Bprind");
}
New public void Nprind ()
{
Console.WriteLine ("I Am a hidden nprind");
}
/* Below is a representation of another hidden method
Public new void Nprind ()
{
Console.WriteLine ("I Am a hidden nprind");
}
*/
public void Cprind ()
{
Console.WriteLine ("I Am the Cprind method");
}
public void Cprind (String str)
{
Console.WriteLine (str+ "I was overloaded");
}
}
Class D
{
static void Main (string []arge)
{
b b=new B ();
B bc=new C ();
C c=new C ();
overriding abstract methods
B.prind ();
overriding virtual methods
B.bprind ();
Bc. Bprind ();
Hide method
B.nprind ();
Bc. Nprind ();
C.nprind ();
Overload
Bc. Cprind ();
Bc. Cprind ("E");
Console.read ();
}
}
Output Result:
I'm class B, I rewrote the abstract method of Class A Prind ()
I am a virtual method in class B, I can be overridden by a derived class that inherits Class B
I rewrote the virtual method of Class B Bprind
Nprind
Nprind
I'm a hidden nprind.
I'm the Cprind method.
E, I've been overloaded.
If looking at this piece of code can be very laborious, so let's listen to the conversation between the classes.
D:a class because you are abstract class, so you can define abstract methods, define the abstract method Prind can be inherited by your subclass rewrite, so class B can rewrite your abstract method Prind.
A: I can be inherited by my subclass B rewrite, which I can also be rewritten by other subclasses, that is to say I used polymorphism.
D: Yes you have used the abstract method involved in polymorphism, because all of your subclasses can rewrite your abstract method, so you conform to the definition of polymorphism, but all of your subclasses have to be aware that overriding your abstract method must use the override keyword, the modifier to be the same, Pass the same number of arguments and types as well as the return value of the method.
D:b class Your own Bprind method is a virtual method can also be inherited by your subclass rewrite, but you and Class A is different, you can also call this virtual method.
B: Why is Bprind a virtual method? It and other methods are no different ah, just a more virtual keyword, it is added the virtual keyword is the virtual method?
D: Yes, the virtual method is necessary to add the virtual keyword, and not add virtual or other keywords we call it a non-virtual method, but also because the virtual method can be overridden, non-virtual methods cannot be rewritten, but note that only inheriting your subclass can override your virtual method.
B: Oh, so the C class can rewrite my bprind this virtual method.
D:c class you not only rewrite the virtual method of the parent class B, but also write the Nprind method in Class B in the hidden method, but also the Cprind method of its own overloaded.
C: I heard you talk to A and B. I know I have rewritten the virtual method, but this hidden method and overloaded the Cprind method, what is the matter?
D: The hiding method is almost the same as the rewrite method, except that the keyword is not overridden and changed to new, and the difference is that new can be either before or after the modifier. Overloading is the same method name used in the same scope, with modifiers, return values, and parameters that can be different.
C: Hide the method I can call it myself, but if I am referring to the parent class, what should I call a method of my or the parent class?
D: You can hide the method yourself, but only the one that is invoked on your own instance is you, and if the parent instance is referring to you, then the method of the parent class is called.
First, let's look at the differences between the overloads and the rewrite from their conversation and code:
Overloading: overriding:
Modifiers can be different must be the same
The number and type of arguments must be different
The return value can be different must be the same
Method names must be the same
Let's take a look at the considerations for abstract methods, hidden methods, and virtual methods:
Abstract method:
1. Overridden in a derived class, defined in the base class.
2. Abstract methods can only be used in abstract classes.
3. Abstract classes cannot be instantiated.
4. Abstract must be used.
Hidden methods:
1. A method name in a derived class that is defined in the base class is the same.
2. The parent class calls the method of the parent class, and the child class calls the method of the subclass.
3. Using the New keyword, virtual methods can also be used.
Virtual method:
1. defined in the parent class, subclasses can be overridden.
2. Use the virtual keyword.
Note: The parent class itself can also be called.
Polymorphism (C #)