To overload a virtual method in a derived class

Source: Internet
Author: User
Tags definition

Let's review the common method overloads first. Common method overloads refer to more than two methods in a class, including hidden inherited methods, with the same name, and the compiler knows under what circumstances the method should be invoked, as long as the parameter type or number of parameters is used.

Overloading a virtual method of a base class is another special form of a function overload. When you redefine this virtual function in a derived class, the method name, the return value type, the number of parameters in the parameter table, the type, and the order must all be exactly the same as the virtual function in the base class. Declaring an overload of a virtual method in a derived class requires that you add the override keyword in the declaration, and you cannot have new,static or virtual modifiers.

Let's use the example of a car class to illustrate the realization of polymorphism.

Program Listing 14-4:

Using System; Class Vehicle//Definition of auto class {public int wheels;///Publicly owned member: Wheel number protected float weight;//protection Member: Weight public Vehicle (int w,float
         g) {wheels=w;
  weight=g;
  public virtual void Speak () {Console.WriteLine ("The W Vehicle is speaking!");

}
}; 
         Class Car:vehicle//Definition sedan class {int passengers;//Private Member: Number of passengers public car (int w,float g,int p): Base (w,g) {wheels=w;
         weight=g;
  Passengers=p;
   public override void Speak () {Console.WriteLine ("The ' car is speaking:di-di!"); } class Truck:vehicle//define truck class {int passengers;//Private Member: Number of passengers float load;//Private Member: Load public truck (int w,float G,int
     P,float l): base (w,g) {wheels=w;
     weight=g;
     Passengers=p;
  Load=l;
    public override void Speak () {Console.WriteLine ("The Truck is speaking:ba-ba!");
    Class Test {public static void Main () {Vehicle v1=new Vehicle ();
    Car c1=new car (4,2,5);
    Truck T1=new Truck (6,5,3,10); V1.
    Speak (); v1=C1; V1.
    Speak (); C1.
  Speak ();
  V1=T1; V1.
     Speak (); T1.
    Speak (); }
}

Analysis of the above example, we can see:

The Speak method in the vehicle class is declared as a virtual method, so this method can be redefined in a derived class.

The Speak method is overloaded separately in the derived class car and truck, and the method prototypes in the derived class and the methods prototypes in the base class must be exactly the same.

In the test class, an instance of the vehicle class is created, V1, and the instance of the car class C1 and the truck class T1.

Run the program, the result should be:

The Vehicle is speaking!
The car is speaking:di-di!
The car is speaking:di-di!
The truck is speaking:ba-ba!
The truck is speaking:ba-ba!

Here, the instance V1 of the vehicle class is given the instance C1 of the car class and the value of the instance T1 of the truck class. During execution, v1 refers to instances of different classes, thus invoking different versions. Here the V1 speak method implements polymorphism and v1. Speak () The implementation of which version, is not determined at the time of compiling the program, but in the dynamic operation of the program, according to the V1 at a certain point of time to determine the type, so also embodies the dynamic polymorphism.

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.