Abstract, virtual, override, and new are the keywords of the four modifier Methods Commonly Used in the class inheritance relationship, which are briefly summarized here.
1. commonly used Chinese names: Abstract abstract method, virtual method, override base class method, new hide base class method, override and new are sometimes called override base class method.
2. Applicability: abstract and virtual are used in the base class (parent class); override and new are used in the derived class (subclass.
3. concept:
Abstract abstract method. It is a null method and has no method body. The derived class must implement this method with override.
Virtual virtual method. If you want or expect this method of the base class to be overwritten (override or new) in a future derived class, this method must be declared as virtual.
Override overrides the Ural method inherited from the base class, which can be understood as removing the old house and building a new house on the original site. The old house can no longer be found (the base class method will never be called ).
New hides the virtual method inherited from the base class, keeps the old house, builds a new house on the side, and wants to live in a new house (called as a derived class object ), want to stay in the old room (called as a base object ).
When a method with the same name as the base class appears in the derived class, and the override or new modifier is not added before this method, the compiler reports a warning, but no error is reported, the actual execution is equivalent to adding new.
4. difference between abstract and virtual: abstract methods have not yet been implemented, and even the base classes cannot be instantiated. They are useless except as a rule or symbol. Virtual is better, if you want to rewrite a derived class, you need to rewrite it. If you don't want to rewrite it, you need to eat Lao Tzu's. In addition, it is better to use less inheritance. The fewer inheritance layers, the better. The fewer new extended functions of the derived class, the better. Virtual fully fits this purpose.
5. Differences between override and new: When the derived class object is used as the base class type, override executes the derived class method, and new executes the base class method. If it is called as a derived class type, it is executed after override or new.
Example of the difference between override and new:
// Define the base class
Class car
{
Public Virtual void describecar ()
{
System. Console. writeline ("four wheels and an engine .");
}
}
// Define the derived classes
Class convertiblecar: Car
{
Public new virtual void describecar ()
{
Base. describecar ();
System. Console. writeline ("a roof that opens up .");
}
}
Class minivan: Car
{
Public override void describecar ()
{
Base. describecar ();
System. Console. writeline ("carries seven people .");
}
}
Public static void testcars1 ()
{
Car car1 = new car ();
Car1.describecar ();
System. Console. writeline ("----------");
Convertiblecar car2 = new convertiblecar ();
Car2.describecar ();
System. Console. writeline ("----------");
Minivan car3 = new minivan ();
Car3.describecar ();
System. Console. writeline ("----------");
}
The output is similar to the following:
Four wheels and an engine.
----------
Four wheels and an engine.
A roof that opens up.
----------
Four wheels and an engine.
Carries seven people.
----------
However, if we declare an array of objects derived from the car base class. This array can store car, convertiblecar, and minivan objects, as shown below:
Public static void testcars2 ()
{
Car [] Cars = new car [3];
Cars [0] = new car ();
Cars [1] = new convertiblecar ();
Cars [2] = new minivan ();
}
Then, use a foreach loop to access each car object contained in the array and call the describecar method, as shown below:
Foreach (car vehicle in cars)
{
System. Console. writeline ("Car object:" + vehicle. GetType ());
Vehicle. describecar ();
System. Console. writeline ("----------");
}
The output of this loop is as follows:
Car object: yourapplication. Car
Four wheels and an engine.
----------
Car object: yourapplication. convertiblecar
Four wheels and an engine.
----------
Car object: yourapplication. minivan
Four wheels and an engine.
Carries seven people.
----------
Note that the convertiblecar description may be different from your expectation. Because the new keyword is used to define this method, the method called is not a derived class method, but a base class method. The minivan object correctly calls the override method and produces the expected results.
Source: http://www.cnblogs.com/ibmfm/archive/2008/12/31/1366153.html