Basic practical skills (04) --------------- inherit Virtual Methods and overwrite, graphic of actual military and boxing skills
I. Form2 inherits the two buttons of Form1 and has a virtual method.
Result
Ii. Code Form1 code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace inherits Virtual Methods and overwrites {public partial class Form1: Form {public Form1 () {InitializeComponent () ;}/// <summary> // display the message, call the virtual method /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void button2_Click (object sender, eventArgs e) {ShowMessage ("hello ");} /// <summary> /// virtual method /// </summary> /// <param name = "message"> </param> protected virtual void ShowMessage (string message) {}}}
Form2 code
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace inherits Virtual Methods and overwrites {public partial class Form2: Form1 {public Form2 () {InitializeComponent ();} protected override void ShowMessage (string message) {MessageBox. show ("I'm button2! ");}}}
Iii. Summary
The parent class executes the click event, calls the virtual method, the virtual method calls the override method, and executes the subclass method.
Virtual methods are mainly used to introduce new methods, while rewriting methods are dedicated to virtual methods inherited from the base class (provide specific implementation of virtual methods ).
Changing the override method of the subclass to an example also displays the parameters of the parent class,
The execution result is as follows:
In a hurry !! C # Can I use ovreride to override a method that is not a virtual method?
Obviously not. Virtual or abstract methods can be rewritten.
Take a closer look:
-------------------------------------
Virtual is used in the base class. specifying a virtual method (attribute) indicates that this method (attribute) can be rewritten.
Override is used in a derived class to overwrite the basic class virtual method (attribute.
The above base classes and derived classes are opposite. B is the base class of C or the derived class of A. B can override the virtual method in A or specify the virtual method for C to rewrite.
You cannot override non-virtual or static methods. The override base method must be virtual, abstract, or override. Why can override be rewritten? Because the override in the base class is actually a rewriting of the base class, because inheritance can be passed, you can also override the override method in the base class.
Override declares that the accessibility of the virtual method cannot be changed. The override and virtual methods must have the same access level modifier.
You cannot use modifiers new, static, virtual, or abstract to modify the override method.
The override attribute declaration must specify the access modifier, type, and name exactly the same as the inherited attribute, and the overwritten attribute must be virtual, abstract, or override.
C # virtual method Rewriting
The new method is independent of inheritance.
The overrride overwrite method is equivalent to calling my dad to keep his nose long.
Nn B = new nn. Of course, the NN object is instantiated.
The problem is not here
Mm a = B;
Here, the problem is critical.
Is to use the parent class to undertake the child Type
Because the F method is an independent Method
So,. F is the parent class independent fangfa, B. F is also independent.
The B. G method is overwritten, so they are all the same.
------
This is easy to understand, but it is a bit difficult to say. You didn't understand it. Maybe it was not clear to you at the time.
------
Msdn.microsoft.com/..9.aspx
There is a related introduction on msdn. You can see it after reading it. If you don't understand it, you can read it again. If you don't understand it, you can just read it again.