Abstract Class vs Concrete Class
Feem ntau ntawm cov khoom siv niaj hnub nrov taw qhia cov lus programming zoo li Java thiab C yog chav kawm. Lawv ua tiav cov khoom taw qhia cov ntsiab lus xws li encapsulation, qub txeeg qub teg thiab polymorphism los ntawm kev siv cov chav kawm. Cov chav kawm yog ib qho piv txwv ntawm cov khoom tiag tiag hauv ntiaj teb. Cov chav kawm tuaj yeem yog pob zeb lossis paub daws teeb meem nyob ntawm theem ntawm kev siv lawv txoj kev ua haujlwm. Ib chav kawm tiav ua tiav tag nrho nws txoj hauv kev. Ib chav kawm paub daws teeb meem tuaj yeem suav hais tias yog ib qho kev txwv ntawm cov chav kawm tsis tu ncua (pob zeb), qhov twg nws yuav muaj qee txoj hauv kev. Feem ntau, cov chav kawm pob zeb raug hu ua (tsuas yog) cov chav kawm.
Qhob Chav Kawm yog dab tsi?
Cov chav kawm default yog chav ua vaj tse. Cov lus tseem ceeb hauv chav kawm yog siv los txhais cov chav kawm (piv txwv li hauv Java). Thiab feem ntau lawv tsuas yog hu ua cov chav kawm (tsis muaj cov lus qhia tshwj xeeb). Cov chav kawm pob zeb piav qhia txog lub tswv yim sawv cev ntawm cov khoom tiag tiag hauv ntiaj teb. Cov chav kawm muaj cov khoom hu ua tus cwj pwm. Cov cwj pwm yog siv raws li lub ntiaj teb thiab piv txwv hloov pauv. Cov txheej txheem hauv cov chav kawm sawv cev lossis txhais tus cwj pwm ntawm cov chav kawm no. Cov txheej txheem thiab cov cwj pwm ntawm cov chav kawm yog hu ua cov tswv cuab ntawm chav kawm. Feem ntau, encapsulation yog ua tiav los ntawm kev ua tus cwj pwm ntiag tug, thaum tsim pej xeem txoj hauv kev uas tuaj yeem siv los nkag rau cov cwj pwm. Ib yam khoom yog qhov piv txwv ntawm chav kawm. Qhov cuab yeej cuab tam tso cai rau tus neeg siv txuas ntxiv cov chav kawm (hu ua sub classes) los ntawm lwm cov chav kawm (hu ua super classes). Polymorphism tso cai rau tus programmer hloov cov khoom ntawm chav kawm nyob rau hauv qhov chaw ntawm cov khoom ntawm nws cov chav kawm super. Feem ntau, cov npe pom hauv qhov teeb meem txhais ncaj qha los ua cov chav kawm hauv qhov kev zov me nyuam. Thiab zoo sib xws, verbs ua txoj kev. Pej xeem, ntiag tug thiab tiv thaiv yog cov kev hloov pauv uas siv tau rau cov chav kawm.
Dab tsi yog Chav Kawm Abstract?
Cov chav kawm Abstract tau tshaj tawm siv cov lus tseem ceeb (piv txwv li hauv Java,). Feem ntau, cov chav kawm Abstract, 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 kom cov menyuam kawm ntawv yuav qhia cov yam ntxwv tsis tiav ntawm cov niam txiv chav kawm thiab kev ua haujlwm tuaj yeem ntxiv los ua kom tiav lawv.
Cov chav kawm Abstract tuaj yeem 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 chav kawm me nyuam siv tag nrho cov txheej txheem Abstract, nws yuav dhau los ua ib chav kawm. 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 los ua qub txeeg qub teg, yuav tsum ua raws li txoj kev txhais raws nraim (tab sis tau kawg tuaj yeem muaj nws tus kheej siv).
Dab tsi yog qhov txawv ntawm Chav Kawm Abstract thiab Chav Ua Vaj Tse?
Cov chav kawm paub daws teeb meem feem ntau muaj ib feem lossis tsis siv. Ntawm qhov tod tes, cov chav kawm ua vaj tse yeej ib txwm muaj kev ua tiav ntawm nws tus cwj pwm. Tsis zoo li cov chav kawm ua vaj tse, cov chav kawm paub daws teeb meem tsis tuaj yeem ua tam sim. Yog li cov chav kawm paub daws teeb meem yuav tsum tau txuas ntxiv txhawm rau ua kom lawv muaj txiaj ntsig. Cov chav kawm paub daws teeb meem yuav muaj cov txheej txheem paub daws teeb meem, tab sis cov chav kawm ua tsis tau. Thaum cov chav kawm paub daws teeb meem txuas ntxiv, txhua txoj hauv kev (ob leeg paub daws teeb thiab pob zeb) tau txais txiaj ntsig. Cov chav kawm qub txeeg qub teg tuaj yeem ua raws li ib qho lossis tag nrho cov txheej txheem. Yog tias tag nrho cov kev paub daws teeb meem tsis raug siv, chav kawm ntawd kuj dhau los ua chav kawm paub daws teeb meem.