C# 虚方法和抽象方法的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14728761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between virtual and abstract methods
提问by iJade
Here is some code from MSDN:
这是来自MSDN 的一些代码:
// compile with: /target:library
public class D
{
public virtual void DoWork(int i)
{
// Original implementation.
}
}
public abstract class E : D
{
public abstract override void DoWork(int i);
}
public class F : E
{
public override void DoWork(int i)
{
// New implementation.
}
}
Can anyone explain the above code with respect to the differences between abstract and virtual methods?
任何人都可以就抽象方法和虚拟方法之间的区别解释上述代码吗?
采纳答案by Dennisch
Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and force the derived classes to override the method.
虚拟方法有一个实现,并为派生类提供覆盖它的选项。抽象方法不提供实现并强制派生类覆盖该方法。
So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN override the method using the override
modifier and provide a custom implementation.
因此,抽象方法中没有实际代码,子类必须覆盖该方法。虚拟方法可以有代码,这通常是某些东西的默认实现,任何子类都可以使用override
修饰符覆盖该方法并提供自定义实现。
public abstract class E
{
public abstract void AbstractMethod(int i);
public virtual void VirtualMethod(int i)
{
// Default implementation which can be overridden by subclasses.
}
}
public class D : E
{
public override void AbstractMethod(int i)
{
// You HAVE to override this method
}
public override void VirtualMethod(int i)
{
// You are allowed to override this method.
}
}
回答by aizaz
First of all you should know the difference between a virtual and abstract method.
首先,您应该知道虚拟方法和抽象方法之间的区别。
Abstract Method
抽象方法
- Abstract Method resides in abstract class and it has no body.
- Abstract Method must be overridden in non-abstract child class.
- 抽象方法驻留在抽象类中,它没有主体。
- 抽象方法必须在非抽象子类中被覆盖。
Virtual Method
虚拟方法
- Virtual Method can reside in abstract and non-abstract class.
- It is not necessary to override virtual method in derived but it can be.
- Virtual method must have body ....can be overridden by "override keyword".....
- 虚方法可以驻留在抽象和非抽象类中。
- 没有必要覆盖派生中的虚拟方法,但可以。
- 虚拟方法必须有主体....可以被“覆盖关键字”覆盖.....
回答by user4230113
an abstract method must be call override in derived class other wise it will give compile-time error and in virtual you may or may not override it's depend if it's good enough use it
抽象方法必须在派生类中调用 override 否则它会给出编译时错误,并且在虚拟中你可能会或可能不会覆盖它取决于它是否足够好使用它
Example:
例子:
abstract class twodshape
{
public abstract void area(); // no body in base class
}
class twodshape2 : twodshape
{
public virtual double area()
{
Console.WriteLine("AREA() may be or may not be override");
}
}
回答by Zubair Saif
Abstract Method:
抽象方法:
If an abstract method is defined in a class, then the class should declare as an abstract class.
An abstract method should contain only method definition, should not Contain the method body/implementation.
- An abstract method must be over ride in the derived class.
如果在类中定义了抽象方法,则该类应声明为抽象类。
抽象方法应仅包含方法定义,不应包含方法主体/实现。
- 抽象方法必须在派生类中被覆盖。
Virtual Method:
虚拟方法:
- Virtual methods can be over ride in the derived class but not mandatory.
- Virtual methods must have the method body/implementation along with the definition.
- 虚拟方法可以在派生类中被覆盖,但不是强制性的。
- 虚拟方法必须具有方法主体/实现以及定义。
Example:
例子:
public abstract class baseclass
{
public abstract decimal getarea(decimal Radius);
public virtual decimal interestpermonth(decimal amount)
{
return amount*12/100;
}
public virtual decimal totalamount(decimal Amount,decimal principleAmount)
{
return Amount + principleAmount;
}
}
public class derivedclass:baseclass
{
public override decimal getarea(decimal Radius)
{
return 2 * (22 / 7) * Radius;
}
public override decimal interestpermonth(decimal amount)
{
return amount * 14 / 100;
}
}