1: Method overrides: The method in the base class is identified with the virtual keyword and then overridden in the inheriting class
(override), so that the methods in the base class have been overridden in the subclass, and the methods in the base class have lost functionality in the subclass
The When a reference to an object of a base class is directed directly to an object of the inheriting class (polymorphism), calling the method is a heavy
Write the method.
2: Method hiding: The New keyword can be used in an inheriting class, regardless of whether the method in the base class uses the virtual keyword (if the new
does not produce an error, but generates a compilation warning) hides the methods in the base class so that hidden is hidden, not as heavy
Write, overriding is that the old method in the base class already does not exist in the subclass, while hiding is the method in the base class and the overridden in the subclass of the new
Methods are present. So when a reference to an object of a base class is directed directly to an object of the inheriting class (polymorphism), calling the method invokes the
The method of the base class.
The code is as follows:
Class BaseClass
{
public void Functiona ()
{
Console.WriteLine ("Basefunctiona = = =");
}
public virtual void functionb ()
{
Console.WriteLine ("basefunctionb = = =");
}
}
Class Derivedclass:baseclass
{
New public void Functiona ()
{
Console.WriteLine ("Derivedfunctiona = = =");
}
public override void Functionb ()
{
Console.WriteLine ("derivedfunctionb = = =");
}
}
Class Program
{
static void Main (string[] args)
{
BaseClass b2 = new DerivedClass ();
B2.functiona ();//basefunctiona = = =
B2.functionb ();//derivedfunctionb = = =
Console.WriteLine ("-----");
BaseClass B = new BaseClass ();
B.functiona ();//basefunctiona = = =
B.functionb ();//basefunctionb = = =
Console.WriteLine ("-----");
DerivedClass d = new DerivedClass ();
D.functiona ();//derivedfunctiona = = =
D.functionb ();//derivedfunctionb = = =
Console.WriteLine ("-----");
}
}
C # hiding methods and overriding methods