抽象方法和虚拟方法有什么区别?在哪些情况下,建议使用抽象方法或虚拟方法?哪一种是最好的方法?
当前回答
上面的大多数示例都使用代码,而且非常好。我不需要补充他们所说的内容,但以下是一个简单的解释,使用了类比而不是代码/技术术语。
简单解释-使用类比进行解释
抽象方法
想想乔治·W·布什。他对士兵们说:“去伊拉克打仗吧”。就这样,他所说的就是必须战斗。他没有具体说明这将如何发生。但我的意思是,你不能只是出去“战斗”:那到底意味着什么?我是用B-52还是我的人字架?这些具体细节留给其他人。这是一种抽象方法。
虚拟方法
大卫·彼得雷乌斯在军队中地位很高。他定义了战斗的含义:
找到敌人中和他。之后喝杯啤酒
问题是这是一种非常通用的方法。这是一个有效的好方法,但有时不够具体。对彼得雷乌斯来说,好事是他的命令有回旋余地和范围——他允许其他人根据他们的特殊要求改变他对“战斗”的定义。
私人职业博客阅读彼得雷乌斯的命令,并根据他的特殊要求获得了实施自己版本的战斗的许可:
找到敌人。朝他的头部开枪。回家吧喝啤酒。
努里·马利基也收到彼得雷乌斯的同样命令。他也要战斗。但他是一名政治家,而不是一名步兵。显然,他不能四处射击他的政治敌人的头部。因为彼得雷乌斯给了他一种虚拟的方法,所以马利基可以根据自己的具体情况,实施自己的战斗方法:
找到敌人。让他以一些捏造的罪名被捕。回家吧喝啤酒。
换言之,虚拟方法提供了样板指令——但这些是一般指令,可以由军队继承人根据他们的具体情况做出更具体的说明。
两者之间的区别
乔治·布什没有证明任何实施细节。这必须由其他人提供。这是一种抽象方法。另一方面,彼得雷乌斯确实提供了实施细节,但他已经允许他的下属,如果他们能想出更好的办法,可以用自己的版本推翻他的命令。
希望这会有所帮助。
其他回答
必须始终重写抽象函数。
因此:
抽象函数-当继承者必须提供自己的实现时虚拟-由继承人决定
我通过对以下课程(从其他答案)进行一些改进,使这一点更简单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestOO
{
class Program
{
static void Main(string[] args)
{
BaseClass _base = new BaseClass();
Console.WriteLine("Calling virtual method directly");
_base.SayHello();
Console.WriteLine("Calling single method directly");
_base.SayGoodbye();
DerivedClass _derived = new DerivedClass();
Console.WriteLine("Calling new method from derived class");
_derived.SayHello();
Console.WriteLine("Calling overrided method from derived class");
_derived.SayGoodbye();
DerivedClass2 _derived2 = new DerivedClass2();
Console.WriteLine("Calling new method from derived2 class");
_derived2.SayHello();
Console.WriteLine("Calling overrided method from derived2 class");
_derived2.SayGoodbye();
Console.ReadLine();
}
}
public class BaseClass
{
public void SayHello()
{
Console.WriteLine("Hello\n");
}
public virtual void SayGoodbye()
{
Console.WriteLine("Goodbye\n");
}
public void HelloGoodbye()
{
this.SayHello();
this.SayGoodbye();
}
}
public abstract class AbstractClass
{
public void SayHello()
{
Console.WriteLine("Hello\n");
}
//public virtual void SayGoodbye()
//{
// Console.WriteLine("Goodbye\n");
//}
public abstract void SayGoodbye();
}
public class DerivedClass : BaseClass
{
public new void SayHello()
{
Console.WriteLine("Hi There");
}
public override void SayGoodbye()
{
Console.WriteLine("See you later");
}
}
public class DerivedClass2 : AbstractClass
{
public new void SayHello()
{
Console.WriteLine("Hi There");
}
// We should use the override keyword with abstract types
//public new void SayGoodbye()
//{
// Console.WriteLine("See you later2");
//}
public override void SayGoodbye()
{
Console.WriteLine("See you later");
}
}
}
抽象函数“只是”签名,没有实现。它在接口中用于声明如何使用类。它必须在其中一个派生类中实现。
虚函数(实际上是方法)也是您声明的函数,应该在继承层次结构类之一中实现。
此类类的继承实例也继承实现,除非您在较低层次结构类中实现它。
在这里,我写了一些示例代码,希望这可能是一个非常实际的例子,可以从非常基本的层面上看到接口、抽象类和普通类的行为。如果您想将其用作演示,也可以在github中找到此代码作为项目:https://github.com/usavas/JavaAbstractAndInterfaceDemo
public interface ExampleInterface {
// public void MethodBodyInInterfaceNotPossible(){
// }
void MethodInInterface();
}
public abstract class AbstractClass {
public abstract void AbstractMethod();
// public abstract void AbstractMethodWithBodyNotPossible(){
//
// };
//Standard Method CAN be declared in AbstractClass
public void StandardMethod(){
System.out.println("Standard Method in AbstractClass (super) runs");
}
}
public class ConcreteClass
extends AbstractClass
implements ExampleInterface{
//Abstract Method HAS TO be IMPLEMENTED in child class. Implemented by ConcreteClass
@Override
public void AbstractMethod() {
System.out.println("AbstractMethod overridden runs");
}
//Standard Method CAN be OVERRIDDEN.
@Override
public void StandardMethod() {
super.StandardMethod();
System.out.println("StandardMethod overridden in ConcreteClass runs");
}
public void ConcreteMethod(){
System.out.println("Concrete method runs");
}
//A method in interface HAS TO be IMPLEMENTED in implementer class.
@Override
public void MethodInInterface() {
System.out.println("MethodInInterface Implemented by ConcreteClass runs");
// Cannot declare abstract method in a concrete class
// public abstract void AbstractMethodDeclarationInConcreteClassNotPossible(){
//
// }
}
}
我在一些地方看到抽象方法的定义如下**
“必须在子类中实现抽象方法”
**我觉得是这样。
如果子类也是抽象的,则不必在子类中实现抽象方法。。
1) 抽象方法不能是私有方法。2) 抽象方法不能在同一抽象类中实现。
我会说。。如果我们要实现一个抽象类,您必须重写基础抽象类中的抽象方法。因为使用重写关键字实现抽象方法。类似于虚拟方法。
虚拟方法不必在继承类中实现。
----------CODE--------------
public abstract class BaseClass
{
public int MyProperty { get; set; }
protected abstract void MyAbstractMethod();
public virtual void MyVirtualMethod()
{
var x = 3 + 4;
}
}
public abstract class myClassA : BaseClass
{
public int MyProperty { get; set; }
//not necessary to implement an abstract method if the child class is also abstract.
protected override void MyAbstractMethod()
{
throw new NotImplementedException();
}
}
public class myClassB : BaseClass
{
public int MyProperty { get; set; }
//You must have to implement the abstract method since this class is not an abstract class.
protected override void MyAbstractMethod()
{
throw new NotImplementedException();
}
}