Qhov txawv ntawm Object thiab Piv Txwv

Qhov txawv ntawm Object thiab Piv Txwv
Qhov txawv ntawm Object thiab Piv Txwv

Video: Qhov txawv ntawm Object thiab Piv Txwv

Video: Qhov txawv ntawm Object thiab Piv Txwv
Video: Qhov Sib Txawv Ntawm Allergy, Flu & Coronavirus Yog Dab Tsi 2024, Kaum ib hlis
Anonim

Object vs Instance

Object Oriented Programming (OOP) yog ib qho ntawm cov kev ua haujlwm nrov tshaj plaws. Hauv OOP, lub hom phiaj yog xav txog qhov teeb meem los daws qhov teeb meem ntawm lub ntiaj teb tiag tiag thiab sawv cev rau qhov teeb meem ntawm cov khoom thiab lawv tus cwj pwm. Ntau hom lus programming uas txhawb nqa qhov tseem ceeb ntawm OOP (hu ua OOP hom lus) muaj chav kawm ua cov cuab yeej programming tseem ceeb. Lawv hu ua class-based. Cov chav kawm yog ib qho piv txwv 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. Hauv cov ntsiab lus yooj yim heev, chav kawm yog ib daim ntawv qhia los yog ib qho qauv rau ib yam khoom ntawm lub neej tiag tiag. Yog li, ib qho khoom yog lub cim xeeb thaiv siv los khaws cov ntaub ntawv tsim nyog raws li daim ntawv qhia no. Piv txwv yog lub cim xeeb thaiv uas hais txog ib yam khoom.

Yam khoom yog dab tsi?

Cov khoom yog cov txiaj ntsig ntawm kev ua tiav ib chav kawm. Instantiation yog tus txheej txheem ntawm kev siv daim phiaj xwm thiab txhais txhua tus cwj pwm thiab tus cwj pwm kom cov khoom tshwm sim sawv cev rau lub neej tiag tiag. Object yog ib qho kev mob siab thiab tsis tu ncua ntawm lub cim xeeb faib los khaws cov ntaub ntawv xws li kev hloov pauv, txoj hauv kev lossis kev ua haujlwm, thiab lwm yam. Khoom yog tsim los ntawm kev siv tus neeg teb xov tooj tshiab, hauv Java programming lus. Piv txwv li, yog tias muaj chav kawm hu ua Tsheb, ces cov hauv qab no tuaj yeem siv los tsim cov khoom ntawm chav kawm Tsheb.

new Car();

Ntawm no, lub tsheb khoom yog tsim los ntawm tus neeg teb xov tooj tshiab thiab xa rov qab rau cov khoom siv. Tus neeg teb xov tooj tshiab nrog rau tus tsim qauv ntawm Chav Tsev Tsheb yog siv los tsim cov khoom tshiab. Lub neej ntev ntawm cov khoom pib los ntawm kev hu mus rau nws tus neeg tsim khoom mus rau lub sijhawm nws raug rhuav tshem. Thaum ib yam khoom tsis raug xa mus, nws yuav raug tshem tawm / rhuav tshem los ntawm tus neeg khaws khib nyiab.

Ib qho piv txwv yog dab tsi?

Instance yog lub cim xeeb thaiv, uas muaj cov lus siv rau ib qho khoom. Hauv lwm lo lus, Instance yuav khaws qhov chaw nyob ntawm qhov pib nco thaiv qhov khoom khaws cia. Qhov tseeb, lub npe ntawm qhov piv txwv tuaj yeem siv los nkag rau qhov pib ntawm qhov chaw nco khoom. Offsets los ntawm lub cim xeeb pib yog xam los ntawm lub tshuab khiav lub sijhawm kom peb tuaj yeem mus rau qhov chaw khaws cov ntaub ntawv tus kheej lossis cov ntaub ntawv xa mus. Ua raws li Java code snipped tuaj yeem siv los tsim ib qho piv txwv ntawm Cov Khoom Siv Tsheb.

Car myCar=New Car();

Raws li tau hais los saum toj no, tus neeg teb xov tooj tshiab tsim lub tsheb khoom thiab xa rov qab cov lus siv rau nws. Qhov kev siv no yog khaws cia rau hauv lub tsheb hom hloov pauv tau myCar. Yog li, myCar yog qhov piv txwv ntawm Cov Khoom Siv Tsheb tsim.

Dab tsi yog qhov txawv ntawm Object thiab Ib Piv Txwv?

Object yog qhov sib txuas ntawm lub cim xeeb uas khaws cov ntaub ntawv tiag tiag uas txawv qhov khoom no los ntawm lwm yam khoom, thaum qhov piv txwv yog siv rau ib yam khoom. Nws yog ib qho thaiv ntawm lub cim xeeb, uas taw qhia rau qhov chaw nyob ntawm qhov chaw nyob ntawm qhov khoom khaws cia. Ob qho piv txwv yuav xa mus rau tib yam khoom. Lub neej ncua sij hawm ntawm ib yam khoom thiab ib qho piv txwv tsis cuam tshuam. Yog li ib qho piv txwv yuav yog null. Thaum txhua qhov piv txwv taw rau ib qho khoom raug tshem tawm, qhov khoom yuav raug rhuav tshem.

Pom zoo: