What are Virtual Methods?

2019-01-11 00:10发布

问题:

Why would you declare a method as "virtual".

What is the benefit in using virtual?

回答1:

The Virtual Modifier is used to mark that a method\property(ect) can be modified in a derived class by using the override modifier.

Example:

class A
{
    public virtual void Foo()
       //DoStuff For A
}

class B : A
{
    public override void Foo()
    //DoStuff For B

    //now call the base to do the stuff for A and B 
    //if required
    base.Foo()
}


回答2:

Virtual allows an inheriting class to replace a method that the base class then uses.

public class Thingy
{
    public virtual void StepA()
    {
        Console.Out.WriteLine("Zing");
    }

    public void Action()
    {
        StepA();
        Console.Out.WriteLine("A Thingy in Action.");
    }
}

public class Widget : Thingy
{
    public override void StepA()
    {
        Console.Out.WriteLine("Wiggy");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Thingy thingy = new Thingy();
        Widget widget = new Widget();

        thingy.Action();
        widget.Action();

        Console.Out.WriteLine("Press any key to quit.");
        Console.ReadKey();
    }
 }

When you run the Program your output will be:

Zing 
A Thingy in Action. 
Wiggy 
A Thingy in Action.

Notice how even though Widget called the Action() method defined at the Thingy level, internally Thingy called Widget's StepA() method.

The basic answer is it gives inheritors of a class more flexibility. Of course, you've got to engineer your class well or it could weak havoc.



回答3:

A virtual method is a type of method where the actual method calls depends on the runtime type of the underlying object.

A non-virtual method is a type of method where the actual method called depends on the reference type of the object at the point of method invocation.



回答4:

Virtual Methods on MSDN

The virtual keyword is used to modify a method or property declaration, in which case the method or the property is called a virtual member. The implementation of a virtual member can be changed by an overriding member in a derived class.

When a virtual method is invoked, the run-time type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member. (For more information on run-time type and most derived implementation, see 10.5.3 Virtual methods.)

By default, methods are non-virtual. You cannot override a non-virtual method.

You cannot use the virtual modifier with the following modifiers:

static abstract override

Virtual properties behave like abstract methods, except for the differences in declaration and invocation syntax.

  • It is an error to use the virtual modifier on a static property.
  • A virtual inherited property can be overridden in a derived class by including a property declaration that uses the override modifier.


回答5:

Virtual methods are similar to abstract methods in base classes except their implementation on derived classes is optional. Also you could put logic in virtual method and override these in derived classes.



回答6:

Even if you don't plan to derive from the class, marking the method virtual may be necessary in order to mock the class. Some mocking frameworks only allow you to mock virtual methods. Note that methods implementing an interface are virtual implicitly.

I use RhinoMocks which has this restriction and have taken to marking my methods virtual by default for just this reason. For me, this is probably the biggest reason to use virtual methods as the cases where inheritance comes into play are much less frequent.



回答7:

In order to be able to override it in inheriting classes.

Check out the MSDN entry for the keyword. That explains it more in depth.



回答8:

A short question, a short answer! Qualify your method as "virtual" if you think you will inherit of the class it belongs to.

A longer answer: "virtual enables you to override, to give another meaning of your method in a derived class.



回答9:

Needless to say, virtual methods come in handy when your code is trying to abide with the Open Closed Principle

Read More about the Open Closed Principle here, Uncle Bob's original OCP whitepaper.

Also pls be aware that methods are not virtual by default in C# unlike Java.



回答10:

Here it is explained clearly with example C# Virtual Method



回答11:

Virtual functions are the functions that doesn't really exists.The derived class can modify the virtual function by overriding it.Virtual functions are one of the way to achieve run time polymorphism

    public class sample {
      public virtual void fun(){
        Console.WriteLine("base sample class \n");
      }
    }
    public class A : sample{
      public override void fun(){
        Console.WriteLine("Class A \n");
      }
    }
    public class B : sample{
      public override void fun(){
        Console.WriteLine("Class B \n");
      }
    }
    class run{
      public static void main(String[] args){
        sample obj = new sample();
        sample obj1 = new A();
        sample obj2 = new B();
        obj.fun();
        obj1.fun();
        obj2.fun();
      }
    }


回答12:

This link will provide you a better understanding with very easy example https://stackoverflow.com/a/2392656/3373865



回答13:

The runtime takes place over compile time.
When you declare a method as virtual, declaring it in derived class require you to add a override or new modifier.
we can see that when TrySpeak. Passing in child and father, both call Speak of father, while TryScream, would call each method.
To understand this, there are some things we should know, in an instance of Child, There are two Scream methods from Child class or Father class. We could either call the Scream from Child class or Father class. Because Virtaul Modifier mark the method so it can be overriding by the derived class, which means even the Scream is called from Father class, it is overriden, it would be defferent if you use new modifier.

import system;
class Father
{
    Speak()
    {
        Console.Writeline("Father is speaking") 
    }
    virtual Scream()
    {
        Console.Writeline("Father is screaming")    
    }
}
class Child: father
{
    Speak()
    {
        Console.Writeline("Child is speaking")  
    }
    override Scream()
    {
        Console.Writeline("Child is screaming") 
    }
}
class APP
{
    public static void Main()
    {
        // We new two instances here
        Father father = new Father();
        Child child = new Child();        
        // Here we call their scream or speak through TryScream or TrySpeak
        TrySpeak(father);
        TrySpeak(child);
        //>>>"Father is speaking"
        //>>>"Father is speaking"
        TryScream(father);
        TryScream(child);
        //>>>"Father is screaming"
        //>>>"Child is screaming"
    }
    // when your method take an Parameter who type is Father
    // You can either pass in a Father instance or
    // A instance of a derived Class from Father
    // which could be Child
    public static void TrySpeak(Father person)
    {
        person.Scream();
    }
    public static void TryScream(Father person)
    {
        person.Speak();
    }
}


回答14:

the difference between virtual and non-virtual methods.

We have two classes; one is a Vehicle class and another is a Cart class. The "Vehicle" class is the base class that has two methods; one is a virtual method "Speed()" and another is a non-virtual method "Average()". So the base class virtual method "Speed()" is overriden in the sub class. We have one more class "Program" (the execution class) that has an entry point where we create an instance of sub class "Cart" and that instance is assigned to the base class "Vehicle" type. When we call virtual and non-virtual methods by both class's instance then according to the run type the instance virtual method implementation is invoked; in other words both class's instances invoke the subclass override method and the non-virtual method invoked is determined based on the instance of the class.

using System;

namespace VirtualExample
{   
    class Vehicle
    {   
       public double distance=0.0;
       public double hour =0.0;
       public double fuel =0.0; 

       public Vehicle(double distance, double hour, double fuel)
       {
           this.distance = distance;
           this.hour = hour;
           this.fuel = fuel;
       }

       public void Average()
       {
           double average = 0.0;
           average = distance / fuel;
           Console.WriteLine("Vehicle Average is {0:0.00}", average);
       }

       public virtual void Speed()
       {
           double speed = 0.0;
           speed = distance / hour;
           Console.WriteLine("Vehicle Speed is {0:0.00}", speed);
       }
    } 

    class Car : Vehicle
    {
        public Car(double distance, double hour, double fuel)
            : base(distance, hour, fuel)
        {
        }
      public void Average()
        {
            double average = 0.0;
            average = distance / fuel;
            Console.WriteLine("Car Average is {0:0.00}", average);
        }

        public override void Speed()
        {
            double speed = 0.0;           
            speed = distance / hour;
            Console.WriteLine("Car Speed is {0:0.00}", speed);
        }
    } 

    class Program
   {
        static void Main(string[] args)
        {
             double distance,hour,fuel=0.0;
             Console.WriteLine("Enter the Distance");
             distance = Double.Parse(Console.ReadLine());
             Console.WriteLine("Enter the Hours");
             hour = Double.Parse(Console.ReadLine());
             Console.WriteLine("Enter the Fuel");
             fuel = Double.Parse(Console.ReadLine());
             Car objCar = new Car(distance,hour,fuel);
             Vehicle objVeh = objCar;
             objCar.Average();
             objVeh.Average();
             objCar.Speed();
             objVeh.Speed();
            Console.Read();
        }       
    }
}

hope to help!