Qhov txawv ntawm Superclass thiab Subclass

Cov txheej txheem:

Qhov txawv ntawm Superclass thiab Subclass
Qhov txawv ntawm Superclass thiab Subclass

Video: Qhov txawv ntawm Superclass thiab Subclass

Video: Qhov txawv ntawm Superclass thiab Subclass
Video: Qhov Txawv Ntawm Lus Hmoob Thiab Lus Mab Mawb 2024, Hlis ntuj nqeg
Anonim

Qhov Tseem Ceeb Qhov txawv - Superclass vs Subclass

In Object Oriented Programming (OOP), lub kaw lus tau ua qauv siv cov khoom. Cov khoom no yog tsim los ntawm chav kawm. Ib chav kawm yog ib daim ntawv qhia lossis piav qhia los tsim ib qho khoom. Object creation kuj hu ua object instantiation. Txhua yam khoom sib txuas lus nrog lwm yam khoom. Ib qho program lossis software tuaj yeem tsim los siv Object Oriented Programming. Kev cuab yeej cuab tam yog lub ntsiab lus tseem ceeb hauv OOP. Nws txhim kho code reusability. Tsis yog siv ib qho kev pab cuam txij thaum pib, nws tso cai rau cov khoom qub thiab cov txheej txheem ntawm cov chav kawm uas twb muaj lawm mus rau chav kawm tshiab. Nws pab ua kom qhov kev zov me nyuam tswj tau ntau dua. Superclass thiab Subclass yog ob nqe lus uas cuam tshuam txog kev qub txeeg qub teg. Kab lus no tham txog qhov sib txawv ntawm Superclass thiab Subclass. Qhov sib txawv tseem ceeb ntawm Superclass thiab Subclass yog Superclass yog cov chav kawm uas twb muaj lawm los ntawm cov chav kawm tshiab tau muab los thaum Subclass yog cov chav kawm tshiab uas tau txais cov khoom thiab cov txheej txheem ntawm Superclass.

Superclass yog dab tsi?

Inheritance, cov chav kawm uas twb muaj lawm los ntawm cov chav kawm tshiab tau muab los yog hu ua Superclass. Nws tseem hu ua chav niam txiv lossis chav kawm hauv paus.

Muaj ntau hom qub txeeg qub teg. Muaj cov duab kos uas siv cov piv txwv hauv qab no. Xav txog A B thiab C raws li cov chav kawm.

Qhov txawv ntawm Superclass thiab Subclass
Qhov txawv ntawm Superclass thiab Subclass

Daim duab 01: Hom Kev Cuam Tshuam

Qhov txawv ntawm Superclass thiab Subclass_Figure 02
Qhov txawv ntawm Superclass thiab Subclass_Figure 02

Daim duab 02: Hybrid Inheritance

Raws li cov duab saum toj no, Superclasses txawv ntawm txhua hom qub txeeg qub teg. Hauv ib qib qub txeeg qub teg, A yog Superclass. Hauv Multilevel inheritance, A yog Superclass rau B thiab B yog Superclass rau C. Hauv Hierarchical Inheritance A yog Superclass rau B thiab C. Hauv ntau qhov qub txeeg qub teg A thiab B yog Superclasses rau C.

Hybrid qub txeeg qub teg yog kev sib txuas ntawm ntau theem thiab ntau yam qub txeeg qub teg. Hauv daim duab sab laug, A yog Superclass rau B, C thiab B, C yog Superclasses rau D. Hauv daim duab sab xis, A yog Superclass rau B thiab B, D yog Superclasses rau C.

Saib cov program hauv qab no sau hauv Java.

Qhov tseem ceeb sib txawv ntawm Superclass thiab Subclass
Qhov tseem ceeb sib txawv ntawm Superclass thiab Subclass

Daim duab 03: Kev Tsim Kho Kev Tsim Kho hauv Java

Raws li cov kev pab cuam saum toj no, chav kawm A muaj sum() thiab sub() txoj kev. Chav B muaj ntau txoj hauv kev. Chav B yog txuas rau chav A. Yog li ntawd, cov khoom thiab cov txheej txheem ntawm chav kawm A yog nkag tau los ntawm chav kawm B. Yog li ntawd, chav kawm A yog Superclass. Hom kev siv ntawm chav kawm B raug coj los tsim cov khoom. Yog li, txhua txoj hauv kev xws li sum(), sub() thiab multiply() nkag tau los ntawm cov khoom. Yog tias Superclass siv hom siv rau kev tsim khoom, cov tswv cuab ntawm chav B tsis tuaj yeem nkag mus tau. e.g. A obj=new B(); Yog li ntawd, Superclass siv tsis tuaj yeem hu rau txoj kev sib npaug () vim tias txoj kev ntawd yog nyob rau hauv chav kawm B.

Subclass yog dab tsi?

Raws li cov duab saum toj no, Subclasses txawv ntawm txhua hom qub txeeg qub teg. Nyob rau hauv Ib Leeg Inheritance, B yog Subclass. Hauv ntau qib qub txeeg qub teg, B yog Subclass ntawm A thiab C yog Subclass ntawm B. Hauv Hierarchical Inheritance B thiab C yog Subclasses ntawm A. Hauv ntau qhov qub txeeg qub teg, C yog Subclass rau A thiab B.

Nyob rau hauv hybrid qub txeeg qub teg, daim duab nyob rau hauv sab laug, B thiab C yog Subclasses ntawm A. D yog cov Subclass ntawm B thiab C. Nyob rau hauv daim duab mus rau sab xis, B yog cov Subclass rau A. C yog cov Subclass. ntawm B thiab D.

Raws li cov kev pab cuam Inheritance saum toj no, chav kawm B yog txuas ntxiv chav kawm A. Yog li ntawd, txhua yam khoom thiab cov txheej txheem ntawm chav kawm A yog nkag tau los ntawm chav kawm B. Chav B yog chav kawm tshiab uas tau txais los ntawm chav kawm A. Nws yog lub npe hu ua lub subclass. Nws tseem hu ua cov chav kawm me nyuam yaus lossis cov chav kawm muab los. Chav B muaj ntau () txoj kev thiab nws tseem tuaj yeem nkag mus rau sum() thiab sub() txoj hauv kev ntawm chav kawm A siv qub txeeg qub teg.

Dab tsi yog qhov zoo sib xws ntawm Superclass thiab Subclass?

Ob leeg muaj feem cuam tshuam nrog Kev Tsim Kho

Qhov txawv ntawm Superclass thiab Subclass yog dab tsi?

Superclass vs Subclass

Thaum siv qhov qub txeeg qub teg, chav kawm uas twb muaj lawm los ntawm cov chav kawm tshiab tau muab los yog Superclass. Thaum siv qhov qub txeeg qub teg, chav kawm uas tau txais cov khoom thiab txoj hauv kev los ntawm Superclass yog Subclass.
Synonyms
Superclass paub tias yog chav kawm hauv paus, chav kawm niam txiv. Subclass hu ua derived class, child class.
A superclass tsis tuaj yeem siv cov khoom thiab cov txheej txheem ntawm Subclass. Ib pawg subclass siv tau cov khoom thiab cov txheej txheem ntawm Superclass.
Ib-theem-Inheritance
Muaj ib Superclass. Muaj ib subclass.
Muaj ib qho Superclass Muaj ntau subclasses.
ntau yam cuab yeej cuab tam
Muaj ntau Superclasses. Muaj ib subclass.

Summary – Superclass vs Subclass

Txoj kev cuab yeej cuab tam yog ib lub tswv yim ntawm OOP. Nws tso cai rau siv cov khoom thiab cov txheej txheem ntawm cov chav kawm uas twb muaj lawm kom nkag tau los ntawm chav kawm tshiab. Cov chav kawm tau txais txiaj ntsig yog Superclass, thiab cov chav kawm tau los yog Subclass. Qhov txawv ntawm Superclass thiab Subclass yog tias Superclass yog cov chav kawm uas twb muaj lawm los ntawm cov chav kawm tshiab tau muab los thaum Subclass yog cov chav kawm tshiab uas tau txais cov khoom thiab cov txheej txheem ntawm Superclass.

Download the PDF Superclass vs Subclass

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 Superclass thiab Subclass

Pom zoo: