Qhov Tseem Ceeb Qhov txawv - Overloading vs Overriding hauv Java
Object-Oriented Programming (OOP) yog qhov tseem ceeb hauv kev txhim kho software. Nws yog ib txoj hauv kev los tsim ib qho kev pab cuam uas siv cov chav kawm thiab cov khoom. Ib chav kawm yog daim ntawv qhia. Nws piav txog yam yuav tsum muaj nyob rau hauv qhov khoom. Nws txhais cov khoom lossis cov cwj pwm thiab cov txheej txheem uas cov khoom yuav tsum muaj. Yog li ntawd, ib qho khoom yog ib qho piv txwv ntawm chav kawm. Cov khoom no sib txuas lus nrog lwm yam khoom. Ib lub tswv yim tseem ceeb ntawm OOP yog Polymorphism. Nws yog lub peev xwm rau ib yam khoom coj los ua ntau yam. Polymorphism tau muab faib ua ob ntu uas yog overloading thiab overriding. Kab lus no tham txog qhov sib txawv ntawm ob qho no hauv Java. Qhov sib txawv tseem ceeb ntawm overloading thiab overriding nyob rau hauv Java yog qhov Overloading yog lub peev xwm los tsim ntau txoj kev ntawm tib lub npe nrog rau kev siv sib txawv thiab Overriding yog muab ib qho kev siv rau ib tug subclass txoj kev uas twb muaj nyob rau hauv lub superclass.
Dab tsi yog kev thauj khoom hauv Java?
Kev Tshaj Tawm yog lub peev xwm los tsim ntau txoj hauv kev ntawm tib lub npe nrog kev siv sib txawv. Xa mus rau Java code hauv qab no.
Daim duab 01: Java program uas piav qhia Kev Tshaj Tawm nrog ntau qhov sib cav
Raws li txoj haujlwm saum toj no, chav kawm A muaj ob txoj hauv kev nrog tib lub npe. Thawj txoj kev sum muaj ob yam. Qhov thib ob sum method muaj peb yam. Thaum tsim ib yam khoom ntawm hom A thiab hu sum (2, 3), nws yuav hu sum method nrog ob tsis uas yog sum (int a, int b) thiab rov 5. Thaum tsim ib yam khoom ntawm hom A thiab hu sum(2, 3, 4), nws yuav hu rau lwm txoj kev suav nrog peb yam uas yog cov lej (int a, int b, int c) thiab xa rov qab 9.
Tus txheej txheem lub npe yog tib yam tab sis tus lej ntawm qhov tsis sib txawv. Nws tuaj yeem pom tias tib yam khoom coj txawv txawv. Lub tswv yim no hu ua overloading. Nws tseem hu ua Static Binding lossis Compiles Time Polymorphism.
Tseem tuaj yeem ua dhau los nrog ntau hom ntaub ntawv. Xa mus rau Java code hauv qab no.
Daim duab 02: Java program uas piav qhia Kev Tshaj Tawm nrog ntau qhov sib cav
Raws li txoj haujlwm saum toj no, chav kawm A muaj ob txoj hauv kev nrog tib lub npe. Qhov sum (int a, int b) txoj kev tau txais ob tus lej suav. Tus lej (ob npaug rau ob npaug b) tau txais ob qhov txiaj ntsig. Thaum tsim cov khoom ntawm hom A thiab hu sum (2, 3), nws yuav hu sum (int a, int b) thiab xa tus nqi 5. Thaum hu sum (3.4, 5.6), nws yuav hu sum (ob npaug rau ob npaug. b) thiab xa rov qab tus nqi 9.0. Hauv qhov piv txwv no, cov txheej txheem muaj tib lub npe, tab sis muaj ntau hom kev sib txawv. Qhov no kuj yog overloading.
Dab tsi yog Kev Tshaj Tawm hauv Java?
Hauv Java, nws muaj peev xwm tsim subclasses nrog cov chav kawm uas twb muaj lawm. Tsis yog tsim cov chav kawm tshiab los ntawm qhov pib, nws muaj peev xwm siv cov khoom thiab cov txheej txheem ntawm cov chav kawm uas twb muaj lawm. Cov chav kawm uas twb muaj lawm yog superclass, thiab cov chav kawm muab tau yog cov subclass. Thaum lub subclass muab ib qho kev siv rau ib txoj kev, uas yog twb nyob rau hauv lub superclass, nws yog hu ua overriding. Xa mus rau Java program hauv qab no.
Daim duab 03: Java program rau overriding
Raws li cov kev pab cuam saum toj no, Chav A muaj cov txheej txheem tso saib (). Chav B txuas ntxiv los ntawm chav kawm A, yog li cov khoom thiab cov txheej txheem ntawm chav kawm A yog nkag tau los ntawm chav kawm B. Chav B muaj cov txheej txheem tso saib () nrog rau kev siv tshwj xeeb. Thaum tsim ib yam khoom ntawm hom, A thiab hu rau cov zaub txoj kev, nws yuav muab cov zis B. Txawm hais tias chav kawm ntawv A muaj ib tug zaub txoj kev, nws yog overridden yuav class B zaub txoj kev. Subclass tab tom siv ib txoj hauv kev uas twb muaj lawm hauv superclass.
Lub tswv yim no yog hom polymorphism thiab hu ua overriding. Nws tseem hu ua Late Binding, Dynamic Binding, Runtime Polymorphism.
Dab tsi zoo sib xws ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv Java?
- Ob leeg yog hom Polymorphism.
- Hauv overloading thiab overriding, txoj kev muaj tib lub npe.
Qhov txawv ntawm Kev Tshaj Tawm thiab Kev Tshaj Tawm hauv Java yog dab tsi?
Overloading vs Overriding hauv Java |
|
Kev Tshaj Tawm hauv Java yog lub peev xwm los tsim ntau txoj hauv kev ntawm tib lub npe nrog kev siv sib txawv. | Overriding nyob rau hauv Java yog muab ib qho kev siv nyob rau hauv subclass txoj kev rau ib txoj kev twb muaj nyob rau hauv lub superclass. |
Parameters | |
Nyob rau hauv overloading, txoj kev muaj tib lub npe tab sis ib tug txawv tus naj npawb ntawm parameters los yog ib tug txawv hom ntawm tsis. | Nyob rau hauv overriding, txoj kev muaj tib lub npe thiab tsis yuav tsum yog tib yam. |
Kev thauj khoom ntau dhau tshwm sim hauv chav kawm. | Kev hla dhau tshwm sim hauv ob chav kawm uas muaj kev sib raug zoo qub txeeg qub teg. |
Synonyms | |
Kev tshaj tawm yog hu ua lub sijhawm sib sau ua ke. | Kev hla dhau yog hu ua lub sijhawm ua haujlwm ntau. |
Summary – Overloading vs Overriding in Java
Polymorphism yog lub tswvyim tseem ceeb hauv Object Oriented Programming. Nws muab lub peev xwm rau ib yam khoom coj los ua ntau yam. Qhov no tuaj yeem yog overloading lossis overriding. Overloading yog lub sijhawm sib sau ua ke polymorphism, thiab overriding yog runtime polymorphism. Lawv muaj txiaj ntsig zoo hauv kev tsim kho software. Qhov txawv ntawm overriding thiab overriding yog tias Overloading yog lub peev xwm los tsim ntau txoj hauv kev ntawm tib lub npe nrog cov kev siv sib txawv thiab Overriding yog muab ib qho kev siv tshwj xeeb hauv subclass method rau ib txoj hauv kev twb muaj nyob rau hauv superclass. Nws tuaj yeem siv ob qho tib si overloading thiab overriding hauv Java.
Download PDF Overloading vs Overriding hauv Java
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 rub tawm PDF version ntawm no Qhov txawv ntawm Kev Tshaj Tawm thiab Tshaj Tawm hauv Java