5. Simple application of object-oriented and WinForm (method overloading, hiding, overriding and virtual methods)

Source: Internet
Author: User

Overloads of the method:

Specifies that a method can have different implementations, but the name of the method is the same. Such as:

// the same man, the   method .  Public int Man (int age,int  name) {  ...} // overloaded  Public int Man (int age ) {  ...}

The presence of overloads can be a common method to use when the main function calls the class, depending on the object's desired flexible invocation.

Methods of hiding:

When a subclass has exactly the same method as the parent class, the method with the same name is hidden from the parent class.

If it is intentionally hidden, you should write the New keyword in the same way, such as:

 class   parent{ public  void   HiDef () { System.Console.WriteLine (    Parent.hidef ()   ); }}  class  child:parent //  { public  new  void   HiDef () { System.Console.WriteLine (    Child.hidef ()   ); }}

The above code runs as "child.hidef ()" when calling the child class.
If you want to invoke the HiDef class of parent, you need to use the base keyword, which is:

base. HiDef (); // calling a method that the parent class is hidden

The hiding of general methods is seldom used.

Methods are overridden with virtual methods:

This is equivalent to hiding the method, but hiding the need to cast, in order to solve the trouble, with the keyword virtual in front of the parent class, indicating that this is a virtual method, the subclass can be overridden by the same method with the keyword override this method, indicating that the parent class with the same name method is overridden.

For example: People have a "talking" feature, which we define in parent Ren:

class ren{  virtualvoid Shuohua ()//The virtual method of the parent class   {     Console.WriteLine (  " can speak ");}  }

We make men a subclass of Ren, and rewrite the words "speak English" on the Inside:

class man:ren{    publicoverridevoid Shuohua ()//Override Parent class method     {       Console.WriteLine (" speak English ");}    }

The "virtual method invocation" attribute of an object-oriented language enables us to perform different operations at run time based on the object type, using only the same statement.

5. Simple application of object-oriented and WinForm (method overloading, hiding, overriding and virtual methods)

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.