在C#中的虚函数的实际应用 [英] Practical usage of virtual functions in c#

查看:183
本文介绍了在C#中的虚函数的实际应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的虚函数的实际使用什么在C#?


解决方案

所以基本上,如果你的祖先类

你想为一个方法有一定的行为。如果你的后代使用相同的方法,但有不同的实现,您可以替换它,如果它有一个虚拟关键字。

 使用系统;
类识别TestClass
{
   公共类尺寸
   {
      公共常量双PI = Math.PI;
      保护双X,Y;
      公共尺寸()
      {
      }
      公共尺寸(双X,双Y)
      {
         this.x = X;
         this.y = Y;
      }      公共虚拟双区()
      {
         返回X * Y;
      }
   }   公共类圆:尺寸
   {
      公共圆(双R):基地(R,0)
      {
      }      公众覆盖双区()
      {
         返回PI * X * X;
      }
   }   类球:尺寸
   {
      公共领域(双R):基地(R,0)
      {
      }      公众覆盖双区()
      {
         返回4 * PI * X * X;
      }
   }   气缸类:尺寸
   {
      公共气缸(双R,双H):基地(R,H)
      {
      }      公众覆盖双区()
      {
         返回到2 * pi * X * X + 2 * PI * X * Y;
      }
   }   公共静态无效的主要()
   {
      双R = 3.0,H = 5.0;
      维C =新圆(R);
      尺寸S =新球体(R);
      尺寸L =新的汽缸(R,H);
      //显示结果:
      Console.WriteLine(圆的面积= {0:F2},c.Area());
      Console.WriteLine(球的面积= {0:F2},s.Area());
      Console.WriteLine(缸面积= {0:F2},l.Area());
   }
}

编辑:在评论结果问题解答
如果我没有在基类中使用虚拟的关键字,将它的工作?

如果您使用覆盖关键字在你的子孙类将无法正常工作。你会产生编译器错误 CS0506 功能1:不能覆盖继承的成员函数2,因为它没有标记为虚,抽象,或覆盖

如果你不使用override你会得到 CS0108 警告'desc.Method()隐藏继承成员base.Method()'使用new关键字隐藏是否意。

要解决这个问题把关键字你前面的方法的隐藏

例如

 新的公共双区()
  {
     返回到2 * pi * X * X + 2 * PI * X * Y;
  }

..和它是强制性的在派生类中重写一个虚方法?的结果
不,如果不覆盖的方法,后代类将使用的方法是从继承。

What 's the practical usage of virtual functions in c#?

解决方案

So basically if in your ancestor class you want a certain behaviour for a method. If your descendent uses the same method but has a different implementation you can override it, If it has a virtual keyword.

using System;
class TestClass 
{
   public class Dimensions 
   {
      public const double pi = Math.PI;
      protected double x, y;
      public Dimensions() 
      {
      }
      public Dimensions (double x, double y) 
      {
         this.x = x;
         this.y = y;
      }

      public virtual double Area() 
      {
         return x*y;
      }
   }

   public class Circle: Dimensions 
   {
      public Circle(double r): base(r, 0) 
      {
      }

      public override double Area() 
      { 
         return pi * x * x; 
      }
   }

   class Sphere: Dimensions 
   {
      public Sphere(double r): base(r, 0) 
      {
      }

      public override double Area()
      {
         return 4 * pi * x * x; 
      }
   }

   class Cylinder: Dimensions 
   {
      public Cylinder(double r, double h): base(r, h) 
      {
      }

      public override double Area() 
      {
         return 2*pi*x*x + 2*pi*x*y; 
      }
   }

   public static void Main()  
   {
      double r = 3.0, h = 5.0;
      Dimensions c = new Circle(r);
      Dimensions s = new Sphere(r);
      Dimensions l = new Cylinder(r, h);
      // Display results:
      Console.WriteLine("Area of Circle   = {0:F2}", c.Area());
      Console.WriteLine("Area of Sphere   = {0:F2}", s.Area());
      Console.WriteLine("Area of Cylinder = {0:F2}", l.Area());
   }
}

Edit: Questions in comment
If I don't use virtual keyword in base class, will it work?

If you use the override keyword in your descendent classes it will not work. You will generate compiler error CS0506 'function1' : cannot override inherited member 'function2' because it is not marked "virtual", "abstract", or "override"

If you don't use the override You'll get the CS0108 warning 'desc.Method()' hides inherited member 'base.Method()' Use the new keyword if hiding was intended.

To get around this put the new keyword in front of the method you are hiding.

e.g.

  new public double Area() 
  {
     return 2*pi*x*x + 2*pi*x*y; 
  }

..and is it compulsory to override a virtual method in derived class?
No, if you don't override the method, the descendent class will use method it is inheriting from.

这篇关于在C#中的虚函数的实际应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆