Qhov Sib txawv ntawm Kev Sib Sau thiab Kev Sib Sau hauv Java

Cov txheej txheem:

Qhov Sib txawv ntawm Kev Sib Sau thiab Kev Sib Sau hauv Java
Qhov Sib txawv ntawm Kev Sib Sau thiab Kev Sib Sau hauv Java

Video: Qhov Sib txawv ntawm Kev Sib Sau thiab Kev Sib Sau hauv Java

Video: Qhov Sib txawv ntawm Kev Sib Sau thiab Kev Sib Sau hauv Java
Video: KEV NTSEEG VAJ TSWV THIAB COJ QUB KEV CAI 05.12.22 2024, Kaum ib hlis
Anonim

Qhov Tseem Ceeb Qhov Sib Txawv - Aggregation vs Composition in Java

Aggregation yog ib qho kev sib koom ua ke ntawm ob yam khoom uas piav txog "muaj-ib" kev sib raug zoo. Cov muaj pes tsawg leeg yog hom tshwj xeeb ntawm kev sib sau ua ke uas qhia txog kev ua tswv cuab. Qhov sib txawv tseem ceeb ntawm kev sib sau ua ke thiab muaj pes tsawg leeg hauv Java yog tias, yog tias cov khoom muaj nyob hauv lub neej tsis muaj tus kheej cov khoom, nws yog kev sib sau ua ke, thiab yog tias cov khoom ntim tsis tuaj yeem nyob tsis muaj qhov muaj nyob ntawm tus kheej, nws yog qhov sib xyaw..

Object-Oriented Programming (OOP) yog qhov tseem ceeb hauv kev txhim kho software. Nws yog siv los ua qauv software siv cov khoom. Cov khoom yog tsim los ntawm cov chav kawm. Ib chav kawm muaj cov khoom thiab cov txheej txheem. Muaj ntau yam khoom hauv software. Txhua yam khoom sib koom ua ke los ntawm kev xa xov. Kev sib raug zoo ntawm ob yam khoom yog lub npe hu ua kev koom tes. Ob qho kev sib sau thiab kev sib xyaw yog ob hom kev koom ua ke. Qhov "muaj-a" kev sib raug zoo piav qhia tias ib qho khoom siv tau lwm yam khoom. Kev sib sau ua ke thiab kev sib sau tuaj yeem ua tiav hauv OOP cov lus txhawb nqa. Yog hais tias cov khoom muaj nyob tau yam tsis muaj qhov muaj nyob ntawm tus kheej, ces kev sib koom ntawm ob yam khoom no yog kev sib sau ua ke. Yog hais tias cov khoom uas muaj tsis tuaj yeem nyob tsis muaj qhov muaj nyob ntawm tus kheej, ces kev sib koom ua ke ntawm ob yam khoom no yog qhov sib xyaw.

Aggregation hauv Java yog dab tsi?

Kev sib sau ua ke yog hom kev koom ua ke. Yog hais tias ib chav kawm muaj ib qho chaw siv, nws yog hu ua aggregation. Aggregation sawv cev rau kev sib raug zoo. Cov khoom siv tub ntxhais kawm tuaj yeem muaj cov khoom xws li student_id, npe, chaw nyob. Cov khoom no tuaj yeem muaj lwm yam khoom hu ua chaw nyob nrog nws cov ntaub ntawv xws li nroog, xeev, lub tebchaws. Hauv qhov xwm txheej no, Tus Tub Kawm Ntawv muaj qhov chaw nyob siv. Nws yog "muaj-a" kev sib raug zoo.

Qhov txawv ntawm Aggregation thiab Composition hauv Java
Qhov txawv ntawm Aggregation thiab Composition hauv Java

Daim duab 01: Cov qhab nia qeb

Qhov txawv ntawm Aggregation thiab Composition hauv Java
Qhov txawv ntawm Aggregation thiab Composition hauv Java

Daim duab 02: Txoj Haujlwm Tseem Ceeb los piav txog Aggregation

Raws li cov kev kawm saum toj no, cov qhab nia hauv chav kawm muaj peb yam khoom uas yog lej, Lus Askiv thiab Kev Tshawb Fawb. Tus Tub Kawm Ntawv muaj ib yam khoom ntawm Marks. Nws muaj nws tus kheej cov khoom uas yog cov qhab nia ntawm lej, lus Askiv thiab science. Nyob rau hauv txoj kev tseem ceeb, ib yam khoom ntawm Marks yog tsim thiab cov cim qhov tseem ceeb raug muab. Cov tub ntxhais kawm cov khoom uas yog s1 tuaj yeem siv cov khoom cim uas yog m1. Yog li ntawd, Cov Tub Ntxhais Kawm thiab Cov Cim muaj qhov "muaj-ib" kev sib raug zoo. Cov khoom Marks tuaj yeem tshwm sim yam tsis muaj Tub Kawm Ntawv Object. Yog li ntawd, nws yog kev sib sau.

Yuav ua li cas hauv Java?

Kev sib sau yog hom kev koom ua ke. Nws yog ib daim ntawv tshwj xeeb ntawm kev sib sau ua ke uas qhia txog kev ua tswv cuab. Nco ntsoov tias muaj ob chav kawm hu ua chav A thiab B. Yog tias cov khoom ntawm chav kawm B tsis tuaj yeem muaj yog tias cov khoom ntawm chav kawm A raug rhuav tshem, ces qhov ntawd yog qhov sib xyaw. Ib phau ntawv muaj ntau nplooj ntawv. Yog tias phau ntawv raug puas tsuaj, nplooj ntawv kuj yuav rhuav tshem. Cov nplooj ntawv khoom tsis tuaj yeem nyob tsis muaj phau ntawv kwv yees. Xa mus rau qhov program hauv qab no.

Qhov txawv ntawm Aggregation thiab Composition hauv Java_Figure 03
Qhov txawv ntawm Aggregation thiab Composition hauv Java_Figure 03

Daim duab 03: Chav Kawm Chav

Qhov txawv ntawm Aggregation thiab Composition hauv Java_Figure 04
Qhov txawv ntawm Aggregation thiab Composition hauv Java_Figure 04

Daim duab 04: Chav Kawm Tsev Kawm Ntawv

Qhov tseem ceeb sib txawv ntawm Aggregation thiab Composition hauv Java
Qhov tseem ceeb sib txawv ntawm Aggregation thiab Composition hauv Java

Daim duab 05: Txoj Haujlwm Tseem Ceeb los piav txog Kev Sau Npe

Raws li cov kev kawm saum toj no, Chav Kawm muaj ob lub zog uas yog lub npe thiab numOfStudent. Lub Tsev Kawm Ntawv yog ib qho ntawm cov khoom siv hauv chav kawm. Nyob rau hauv txoj kev tseem ceeb, ob chav kawm khoom raug tsim. Cov no ntxiv rau 'chav kawm'. Cov 'chav kawm' no raug xa mus rau lub tsev kawm ntawv khoom. Thaum kawg, cov npe hauv chav kawm thiab cov tub ntxhais kawm raug luam tawm los ntawm iterating los ntawm kev sau. Yog lub tsev kawm ntawv puas raug puas tsuaj, cov khoom hauv chav kawm kuj yuav rhuav tshem. Qhov no yog ib qho piv txwv ntawm kev sib xyaw. Nws kuj muaj qhov 'muaj-a' kev sib raug zoo thiab tseem hais txog kev ua tswv cuab.

Kev sib raug zoo ntawm kev sib sau thiab kev sib sau hauv Java yog dab tsi?

Aggregation thiab Composition yog ob hom Association thiab Composition yog ib hom tshwj xeeb ntawm Aggregation. Kev sib sau yog ib pawg ntawm Aggregation

Dab tsi yog qhov txawv ntawm Kev Sib Sau thiab Kev Sib Sau hauv Java?

Aggregation vs Composition in Java

Kev sib sau ua ke yog kev sib koom ua ke ntawm ob yam khoom uas piav txog "muaj" kev sib raug zoo. Kev sib sau yog ib hom kev sib sau tshwj xeeb uas txhais tau tias yog tswv.
Kev sib sau yog siv thaum ib yam khoom siv lwm yam khoom. Kev sib sau yog siv thaum ib qho khoom muaj lwm yam khoom.
Txoj kev rau khoom
Hauv kev sib sau, kev rhuav tshem cov khoom ntiag tug yuav tsis cuam tshuam rau cov khoom muaj. Hauv kev sib xyaw, rhuav tshem cov khoom ntiag tug yuav cuam tshuam rau cov khoom muaj.

Summary – Aggregation vs Composition in Java

Aggregation thiab Composition yog ob lub ntsiab lus hauv OOP. Qhov "muaj-a" kev sib raug zoo piav qhia tias ib qho khoom siv tau lwm yam khoom. Kev sib sau ua ke yog kev sib koom ua ke ntawm ob yam khoom uas piav qhia txog kev sib raug zoo "muaj-a". Kev sib xyaw yog ib hom kev sib sau ua ke uas qhia txog kev ua tswv cuab. Qhov txawv ntawm kev sib sau ua ke thiab muaj pes tsawg leeg hauv Java yog tias, yog tias cov khoom muaj peev xwm muaj nyob tau yam tsis muaj qhov muaj nyob ntawm tus kheej cov khoom nws yog kev sib sau thiab yog tias cov khoom ntim tsis tuaj yeem tshwm sim yam tsis muaj lub neej ntawm tus kheej, nws yog qhov sib xyaw.

Pom zoo: