Virtual vs Abstract
Virtual thiab Abstract yog ob lo lus tseem ceeb siv nyob rau hauv feem ntau Object Oriented (OO) programming lus xws li Java thiab C. Txawm hais tias muaj qhov sib txawv me ntsis ntawm qhov nws txhais tau ntau hom lus, ob qho tib si Virtual thiab Abstract cov ntsiab lus muab kev nkag siab ntawm kev siv ib nrab rau cov koom haum nws txuas nrog.
Abstract
Feem ntau, Cov chav kawm paub daws teeb meem, tseem hu ua Abstract Base Classes (ABC), tsis tuaj yeem ua tam sim (ib qho piv txwv ntawm chav kawm ntawd tsis tuaj yeem tsim). Yog li, cov chav kawm Abstract tsuas yog muaj txiaj ntsig kom muaj yog tias cov lus programming txhawb nqa qub txeeg qub teg (muaj peev xwm los tsim cov subclasses los ntawm kev nthuav dav hauv chav kawm). Cov chav kawm Abstract feem ntau yog sawv cev rau lub tswv yim paub daws teeb los yog qhov chaw nrog ib feem lossis tsis siv. Yog li ntawd, cov chav kawm Abstract ua raws li niam txiv cov chav kawm los ntawm cov chav kawm me nyuam tau muab los, yog li cov menyuam kawm ntawv yuav qhia cov yam ntxwv tsis tiav ntawm cov niam txiv hauv chav kawm thiab kev ua haujlwm ntxiv tuaj yeem ua kom tiav lawv. Cov chav kawm Abstract tej zaum yuav muaj cov txheej txheem Abstract. Subclasses txuas ntxiv ib chav kawm paub daws teeb meem tuaj yeem ua raws li cov txheej txheem no (kuj paub) Abstract. Yog hais tias tus me nyuam chav kawm siv tag nrho cov txheej txheem Abstract, nws yog ib chav kawm tiav. Tab sis yog tias tsis yog, chav kawm me nyuam kuj dhau los ua chav kawm Abstract. Tag nrho qhov no txhais tau li cas yog tias, thaum tus programmer xaiv cov chav kawm ua Abstract, nws tau hais tias chav kawm yuav ua tsis tiav thiab nws yuav muaj cov ntsiab lus uas yuav tsum tau ua kom tiav los ntawm kev txais cov subclasses. Qhov no yog ib txoj hauv kev zoo los tsim ib daim ntawv cog lus ntawm ob tus programmers, uas ua kom yooj yim cov haujlwm hauv kev txhim kho software. Tus programmer, uas sau cov cai rau qub txeeg qub teg, yuav tsum ua raws li cov txheej txheem txhais raws nraim (tab sis tau kawg tuaj yeem muaj nws tus kheej siv). Hauv Java thiab C, Cov chav kawm paub daws teeb meem thiab cov txheej txheem tau tshaj tawm siv cov lus tseem ceeb.
Virtual
Virtual method/functions muab lub peev xwm los xaiv nws tus cwj pwm los ntawm cov chav kawm qub (siv lub luag haujlwm nrog tib lub npe). Lub tswv yim ntawm Virtual muaj nuj nqi yog qhov tseem ceeb hauv cov xwm txheej hauv qab no. Piv txwv tias ib chav kawm yog muab los ntawm cov menyuam hauv chav kawm, tom qab ntawd thaum twg ib qho khoom siv ntawm chav kawm muab tau los, nws yuav xa mus rau ib qho khoom ntawm chav kawm hauv paus lossis chav kawm muab tau. Tab sis, txoj kev hu tus cwj pwm tuaj yeem tsis meej yog tias cov txheej txheem hauv chav kawm tau dhau mus. Yog li, txhawm rau daws qhov tsis meej pem no, Virtual lo lus tseem ceeb yog siv. Yog hais tias tus txheej txheem raug cim Virtual, ces lub derived chav kawm ntawv muaj nuj nqi hu ua (yog tias muaj) los yog lwm yam lub hauv paus chav kawm ntawv muaj nuj nqi hu ua. Piv txwv li, hauv C ++, Virtual lo lus tseem ceeb yog siv raws nraim rau lub hom phiaj no. Hauv C, lo lus tseem ceeb Virtual yog siv nyob rau hauv ib txoj kev zoo sib xws, tab sis ntxiv, lo lus tseem ceeb override yuav tsum tau siv los hloov tag nrho cov txheej txheem overridden. Tab sis hauv Java, tsis muaj lo lus tseem ceeb Virtual. Txhua txoj kev tsis zoo li qub raug suav tias yog Virtual. Virtual functions uas tsis muaj lub cev hu ua Pure Virtual functions. Hauv Java thiab C, cov kev paub daws teeb meem yog qhov tseeb Pure Virtual.
Qhov txawv ntawm Virtual thiab Abstract
Txawm hais tias Abstract thiab Virtual yog ob lo lus / lub tswv yim uas muab lub ntsiab lus ntawm kev ua tsis tiav rau nws cov koom haum cuam tshuam, lawv muaj qhov sib txawv. Cov txheej txheem Abstract (uas yuav tsum tau txhais hauv cov chav kawm Abstract) tsis muaj kev siv txhua, thaum Virtual txoj kev yuav muaj kev siv. Yog tias Abstract txoj kev txuas ntxiv los ntawm cov chav kawm ua vaj tse, txhua txoj hauv kev Abstract yuav tsum tau siv, thaum cov txheej txheem Virtual tau los yog tsis tuaj yeem hla dhau.