3 回答
TA贡献1719条经验 获得超6个赞
抽象函数不能具有功能。您基本上是在说,任何子类都必须提供自己的该方法的版本,但是它太笼统了,甚至无法尝试在父类中实现。
虚函数基本上是在说看,这里的功能对于子类来说可能足够好,也可能不够好。因此,如果足够好,请使用此方法;否则,请覆盖我并提供您自己的功能。
TA贡献1829条经验 获得超7个赞
抽象函数没有实现,只能在抽象类上声明。这迫使派生类提供实现。
虚函数提供了默认实现,它可以存在于抽象类或非抽象类上。
因此,例如:
public abstract class myBase
{
//If you derive from this class you must implement this method. notice we have no method body here either
public abstract void YouMustImplement();
//If you derive from this class you can change the behavior but are not required to
public virtual void YouCanOverride()
{
}
}
public class MyBase
{
//This will not compile because you cannot have an abstract method in a non-abstract class
public abstract void YouMustImplement();
}
TA贡献1858条经验 获得超8个赞
只有
abstract班级可以有abstract成员。一个非
abstract类从继承abstract类必须override的abstract成员。一个
abstract成员是隐式virtual。一个
abstract成员不能提供任何实现(abstract被称为pure virtual在某些语言)。
添加回答
举报
