Learn to remember a little bit so that you do not forget it ..
Father
Code
Class Father
{
/// <Summary>
/// The virtual method can be derived and overwritten.
/// The father told his son that you can learn this skill, but you need to have your own "innovation or new" after learning it )"
/// </Summary>
Public virtual void virtualMethod ()
{
Console. WriteLine ("Father Virtual Method ");
}
/// <Summary>
/// If there is a method with the same name in the derived class, you can use the new keyword to hide the parent class method. Of course, new can also be used to instantiate the class.
/// In fact, it can be understood that the son has learned a similar skill as his father. For example, the father sang a popular brother and the son sang a popular song.
/// </Summary>
Public void newMethod ()
{
Console. WriteLine ("Father New Method ");
}
/// <Summary>
/// The derived class cannot be inherited.
/// In fact, it can be understood that the father does not want his son to learn, such as the father's robbery and does not want his son to learn...
/// </Summary>
Private void privateMethod ()
{
Console. WriteLine ("Father Private Method ");
}
/// <Summary>
/// The derived class can inherit
/// Actually, it can be understood as the skill that the father passes on to his son.
/// </Summary>
Public void publicMethod ()
{
Console. WriteLine ("Father Public Method ");
}
/// <Summary>
/// PublicMethod overload
/// Overload: different parameters with the same name
/// It is also the father's skill to pass on to his son.
/// </Summary>
Public void publicMethod (string message)
{
Console. WriteLine ("Father Public Method" + message );
}
}
Son
Code
Class Son: Father
{
/// <Summary>
/// Rewrite the virtual method of the base class
/// It can be understood that the son has learned his father's skills and has his own intellectual property rights.
/// </Summary>
Public override void virtualMethod ()
{
Console. WriteLine ("Son Virtual Method ");
}
/// <Summary>
/// Hide the cognominal method of the parent class
/// It can be understood that the son has learned his father's skills and has his own intellectual property rights.
/// </Summary>
Public new void newMethod ()
{
Console. WriteLine ("Son New Method ");
}
}
Main
Code
Class Program
{
Static void Main (string [] args)
{
Son son = new Son ();
Son. virtualMethod ();
Son. newMethod ();
Son. publicMethod ();
Console. WriteLine ();
Father father = new Father ();
Father. virtualMethod ();
Father. newMethod ();
Father. publicMethod ();
Console. WriteLine ();
Father father_son = new Son ();
Father_son.virtualMethod (); // method of the base class completely overwritten by override
Father_son.newMethod (); // new is only hidden. When a transformation object appears, the father's method is called.
Father_son.publicMethod ();
}
}
Output
Son Virtual Method
Son New Method
Father Public Method
Father Virtual Method
Father New Method
Father Public Method
Son Virtual Method
Father New Method
Father Public Method