抽象方法和虚拟方法有什么区别?在哪些情况下,建议使用抽象方法或虚拟方法?哪一种是最好的方法?
当前回答
抽象方法没有实现。它在父类中声明。子类负责实现该方法。
虚拟方法应该在父类中有一个实现,它有助于子类选择是使用父类的该实现,还是为子类中的该方法自己有一个新的实现。
其他回答
抽象函数(方法):
● 抽象方法是用关键字abstract声明的方法。
● 它没有身体。
● 它应该由派生类实现。
● 如果方法是抽象的,那么类应该是抽象的。
虚拟函数(方法):
● 虚方法是用关键字virtual声明的方法,它可以通过使用override关键字由派生类方法重写。
● 是否重写它取决于派生类。
我通过对以下课程(从其他答案)进行一些改进,使这一点更简单:
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");
}
}
}
绑定是将名称映射到代码单元的过程。
后期绑定意味着我们使用名称,但延迟映射。换言之,我们首先创建/提及名称,然后让后续流程处理代码到该名称的映射。
现在考虑:
与人类相比,机器确实擅长搜索和排序与机器相比,人类确实擅长发明和创新
因此,简单的答案是:virtual是机器(运行时)的后期绑定指令,而abstract是人类(程序员)的后期约束指令
换句话说,虚拟意味着:
亲爱的运行时,通过执行您最擅长的操作将适当的代码绑定到此名称:搜索
抽象意味着:
亲爱的程序员,请尽你所能将适当的代码绑定到这个名字上:发明
为完整起见,重载意味着:
“亲爱的编译器,通过做你最擅长的事情,将适当的代码绑定到这个名称:排序”。
从面向对象的常规视图:关于抽象方法:当你把一个抽象方法放在父类中时,实际上你在对子类说:嘿,注意你有一个这样的方法签名。如果你想使用它,你应该实现你自己的!关于虚拟函数:当你在父类中放置一个虚拟方法时,你会对派生类说:嘿,这里有一个功能可以为你做些什么。如果这对你有用,就使用它。如果没有,重写它并实现你的代码,甚至你可以在代码中使用我的实现!这是关于通用OO中这两个概念之间不同的一些哲学
我在一些地方看到抽象方法的定义如下**
“必须在子类中实现抽象方法”
**我觉得是这样。
如果子类也是抽象的,则不必在子类中实现抽象方法。。
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();
}
}