Qhov Tseem Ceeb Qhov txawv - Chav vs Interface
Object-Oriented Programming (OOP) yog ib qho piv txwv zoo hauv kev txhim kho software. Nws pab coj cov xwm txheej hauv ntiaj teb tiag tiag rau kev ua haujlwm siv cov chav kawm thiab cov khoom siv. Ib chav kawm yog ib daim phiaj los tsim ib yam khoom. Tus programmer tuaj yeem tsim cov chav kawm nrog cov khoom thiab cov txheej txheem. Ib tug tub kawm ntawv thiab tus kws qhia ntawv yog khoom. Tsim ib yam khoom hu ua instantiation. OOP kuj tseem siv interfaces. Interfaces thiab chav kawm yuav zoo sib xws, tab sis lawv muaj qhov sib txawv. Kab lus no tham txog qhov sib txawv ntawm chav kawm thiab ib qho interface. Qhov sib txawv tseem ceeb ntawm chav kawm thiab ib qho kev sib txuas yog tias chav kawm yog hom kev siv uas yog daim ntawv qhia rau instantiate ib yam khoom thaum lub interface yog hom siv uas tsis tuaj yeem siv los ua ib qho khoom.
Hnub yog dab tsi?
Hauv OOP, txhua yam raug suav tias yog khoom. Nws tsis tuaj yeem tsim ib qho khoom tsis muaj chav kawm. Ib chav kawm yog ib daim phiaj los tsim ib yam khoom. Thaum tsim ib lub tsev, tus kws kes duab vajtse kos lub phiaj xwm. Txoj kev npaj zoo ib yam li chav kawm. Lub tsev zoo ib yam li cov khoom. Chav kawm yog txoj kev npaj tsim ib qho khoom. Ib yam khoom yog tsim los ntawm chav kawm.
chav kawm muaj cov khoom thiab cov txheej txheem. Tus menyuam kawm ntawv tuaj yeem muaj cov khoom xws li npe, qib, tus lej ntsuas. Cov tub ntxhais kawm tuaj yeem muaj txoj hauv kev xws li nyeem ntawv, taug kev, kawm. Ib chav kawm yog tsim nrog cov khoom tsim nyog thiab txoj hauv kev.
Cov lus qhia rau kev tsim cov chav kawm hauv ntau hom lus programming yog raws li hauv qab no. Nws yog tsim los siv cov lus tseem ceeb hauv chav kawm.
class_name {
// khoom
//methods
}
Programming lus xws li C thiab Java ua raws li cov syntax zoo sib xws los tsim cov khoom siv chav kawm. Xav tias lub npe chav kawm yog Student.
Student s1=New Student ();
No s1 yog qhov khoom. Lo lus "tshiab" yog siv los faib lub cim xeeb rau cov khoom. Ib chav kawm kuj muaj tus tsim tsim los pib cov khoom lossis cov hloov pauv.
Class cov tswv cuab xws li cov khoom thiab cov txheej txheem muaj kev hloov kho. Access specifiers piav qhia txog kev nkag mus tau thiab pom ntawm cov tswv cuab mus rau lwm chav kawm. Cov tswv cuab ntawm chav kawm tuaj yeem muaj kev nkag mus rau cov khoom tshwj xeeb xws li pej xeem, ntiag tug thiab tiv thaiv. Cov tswv cuab pej xeem tuaj yeem nkag tau los ntawm lwm cov chav kawm. Cov tswv cuab ntiag tug tsuas yog siv tau rau hauv chav kawm xwb. Cov tswv cuab tiv thaiv tuaj yeem nkag mus tau hauv chav kawm thiab cov chav kawm muaj feem cuam tshuam.
Dab tsi yog Interface?
Abstraction yog tus ncej ntawm Object Oriented programming. Nws yog los nkaum cov ntsiab lus ntawm kev siv thiab tso saib cov haujlwm ua haujlwm rau tus neeg siv. Abstraction yog ua tiav nrog cov chav kawm paub daws teeb thiab interfaces. Ib txoj kev paub daws teeb meem tsis muaj kev siv. Ib chav kawm uas muaj tsawg kawg yog ib txoj kev paub daws teeb meem yog hu ua chav kawm paub daws teeb.
Thaum muaj ob chav kawm paub daws teeb meem, cov kev tshaj tawm hauv cov chav kawm yuav tsum tau ua. Ib chav kawm tshiab yog siv los ua cov txheej txheem ntawd. Yog tias ob chav kawm muaj tib txoj kev, nws yuav ua rau muaj teeb meem tsis meej pem. Yog li ntawd, cov lus programming xws li Java thiab C muaj qhov cuam tshuam.
Interfaces tsuas muaj cov lus tshaj tawm ntawm txoj hauv kev. Tsis muaj txoj hauv kev los siv. Tsis tas li ntawd, interfaces tsis tuaj yeem siv los tsim cov khoom. Lawv siv los txhawb ntau yam qub txeeg qub teg thiab kom ruaj ntseg code.
Cov syntax ntawm Interface yog raws li hauv qab no. Interface siv lo lus tseem ceeb "interface".
interface_name{
type method1(parameter_list);
type method2(parameter_list);
}
Raws li saum toj no, interfaces tsuas muaj cov lus tshaj tawm. Tsis muaj kev txhais. Yog li, interfaces tsis tuaj yeem cuam tshuam cov khoom. Nws tsuas yog muab qhov pom tsis meej ntawm qhov interface yog dab tsi. Cov txheej txheem tshaj tawm hauv lub interface tuaj yeem siv los ntawm ib lossis ntau chav kawm. Ib chav kawm siv lo lus tseem ceeb "ua" los siv lub interface. Saib hauv qab piv txwv sau siv Java.
Daim duab 01: Kev Pabcuam siv Interfaces
Raws li cov kev pab cuam saum toj no, A thiab B yog qhov sib cuam tshuam. Interface A muaj ib txoj kev tshaj tawm uas yog tus lej (). Interface B muaj ib txoj kev tshaj tawm sub(). Chav C yog siv ob qho tib si interfaces uas yog A thiab B. Yog li, chav kawm C txhais ob qho tib si sum() thiab sub() txoj kev. Tom qab tsim cov khoom ntawm hom C, nws tuaj yeem hu rau ob txoj hauv kev sum() thiab sub().
Txoj kev tshaj tawm hauv lub interface yuav tsum yog pej xeem ib txwm vim tias cov chav kawm siv tau txhais lawv. Ib qho interface kuj tuaj yeem tau txais los ntawm lwm tus interface.
Qhov Zoo sib xws ntawm Chav Kawm thiab Interface yog dab tsi?
- Ob leeg yog hom siv.
- Ob leeg cuam tshuam rau Object-Oriented Programming.
Qhov txawv ntawm Chav Kawm thiab Interface yog dab tsi?
Class vs Interface |
|
Ib chav kawm yog hom kev siv uas yog daim ntawv qhia los tsim ib qho khoom. | Ib qho interface yog hom kev siv uas tsis tuaj yeem ua sai sai. |
Object Instantiation | |
Ib chav kawm yog siv los ua qhov khoom tam sim. | Ib qho kev sib tham tsis tuaj yeem ua sai sai vim tias cov txheej txheem tsis tuaj yeem ua ib yam dab tsi. |
Ib chav kawm muaj tus tsim, txhawm rau pib qhov hloov pauv. | Ib qho interface tsis muaj tus tsim vim tias lawv tsis tshua muaj qhov sib txawv los pib. |
lo lus tseem ceeb | |
chav kawm siv lo lus tseem ceeb "chav". | Ib qho interface siv lo lus tseem ceeb "interface". |
Access Specifier | |
Cov tswv cuab ntawm chav kawm tuaj yeem yog tus kheej, pej xeem thiab tiv thaiv. | Cov tswv cuab ntawm lub interface yuav tsum yog pej xeem ib txwm vim cov chav kawm siv tau txhais lawv. |
Summary – Chav vs Interface
Chav kawm thiab Interfaces tau siv dav hauv Object Oriented Programming. Qhov sib txawv ntawm ib chav kawm thiab ib qho kev sib txuas yog tias chav kawm yog hom kev siv uas yog daim ntawv qhia rau instantiate ib yam khoom thiab interface yog hom siv uas tsis tuaj yeem siv los ua ib qho khoom. Ib chav kawm tuaj yeem siv ntau qhov kev sib tshuam. Tab sis nws tsuas tuaj yeem txuas tau ib qho superclass. Hauv kev sib txuas tuaj yeem tau txais ntau lub interfaces tab sis tsis tuaj yeem ua tiav. Ob leeg muaj lawv qhov tseem ceeb. Tus programmer siv tau raws li kev tsim software.
Download the PDF Class vs Interface
Koj tuaj yeem rub tawm PDF version ntawm kab lus no thiab siv nws rau offline hom phiaj raws li daim ntawv ceeb toom. Thov download PDF version ntawm no Qhov txawv Ntawm Chav Kawm thiab Interface