Learning logs-virtual, override, new, overload

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.