3 回答

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
抽象函數(shù)不能具有功能。您基本上是在說,任何子類都必須提供自己的該方法的版本,但是它太籠統(tǒng)了,甚至無法嘗試在父類中實(shí)現(xiàn)。
虛函數(shù)基本上是在說看,這里的功能對于子類來說可能足夠好,也可能不夠好。因此,如果足夠好,請使用此方法;否則,請覆蓋我并提供您自己的功能。

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
抽象函數(shù)沒有實(shí)現(xiàn),只能在抽象類上聲明。這迫使派生類提供實(shí)現(xiàn)。
虛函數(shù)提供了默認(rèn)實(shí)現(xiàn),它可以存在于抽象類或非抽象類上。
因此,例如:
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貢獻(xiàn)1858條經(jīng)驗(yàn) 獲得超8個(gè)贊
只有
abstract
班級可以有abstract
成員。一個(gè)非
abstract
類從繼承abstract
類必須override
的abstract
成員。一個(gè)
abstract
成員是隱式virtual
。一個(gè)
abstract
成員不能提供任何實(shí)現(xiàn)(abstract
被稱為pure virtual
在某些語言)。
添加回答
舉報(bào)