Vim li cas enum constructor private?
Vim li cas enum constructor private?

Video: Vim li cas enum constructor private?

Video: Vim li cas enum constructor private?
Video: #70 Enum Class in Java 2024, Hlis ntuj nqeg
Anonim

Koj tsis tuaj yeem muaj pej xeem ib constructor . Koj xav tau qhov no tus tsim ua tus kheej , vim enums txhais cov txheej txheem tas nrho (piv txwv li EN_US, EN_UK, FR_FR, FR_BE). Yog tias tus tsim yog cov pej xeem tuaj yeem tsim muaj txiaj ntsig ntau dua (piv txwv li qhov tsis raug / tsis lees paub qhov tseem ceeb xws li XX_KK, thiab lwm yam).

Hais txog qhov no, enum puas muaj tus tsim?

Qhov no yog vim enum yog Java muaj cov nqi tas li. Yog li, tsis muaj qhov taw tes rau pej xeem lossis tiv thaiv tus tsim raws li koj tsis tuaj yeem tsim qhov piv txwv ntawm enum . Tsis tas li ntawd, nco ntsoov tias sab hauv enum hloov mus rau chav kawm raws li hauv qab no. Raws li peb ua tau tsis tsim enum cov khoom qhia meej yog li peb ua tau 't lus ib constructor ncaj qha.

Tsis tas li ntawd, dab tsi yog qhov txawv ntawm tus enum constructor thiab ib tug tsis tu ncua class constructor? Los teb cov lus nug: qhov tseem ceeb, tsis muaj qhov sib txawv ntawm ob txoj kev. Txawm li cas los xij, enum tsim muab rau koj nrog qee qhov kev txhawb nqa ntxiv xws li values(), valueOf(), thiab lwm yam uas koj yuav tsum tau sau rau koj tus kheej nrog rau chav kawm -nrog-private- tus tsim mus kom ze.

Dhau li ntawm no, lub hom phiaj ntawm enums yog dab tsi?

Cov enum yog siv los tshaj tawm ib daim ntawv teev npe integer constants. Nws tuaj yeem txhais tau tias siv lub enum lo lus tseem ceeb ncaj qha rau hauv lub npe, chav kawm, lossis qauv. Cov enum yog siv los muab lub npe rau txhua qhov tas li kom tus lej tas mus li tuaj yeem raug xa mus siv nws lub npe. Piv txwv: enum.

Can enums tau instantiated?

Enums tsis txhawb pej xeem constructors thiab li no, ua tsis tau instantiated . Enums yog rau thaum koj muaj ib tug taag txheej uas muaj feem xyuam. Muaj ib qho piv txwv yuav raug tsim rau txhua tus enum tas li.

Pom zoo: