Tsis yog, Cov neeg tsim khoom tuaj yeem yog pej xeem, ntiag tug, tiv thaiv lossis ua haujlwm(tsis muaj kev hloov pauv txhua). Ua ib yam dab tsi ntiag tug tsis txhais hais tias tsis muaj leej twg nkag tau rau nws. Nws tsuas yog txhais tau tias tsis muaj leej twg nyob sab nraud ntawm chav kawm tuaj yeem nkag mus tau. Yog li private constructor tseem ceeb heev.
Cov neeg tsim khoom puas tuaj yeem yog tus kheej hauv Java?
Ib tus tsim tus kheej hauv Java yog siv los txwv kev tsim khoom. Nws yog ib tug tshwj xeeb piv txwv constructor siv nyob rau hauv static member-tsuas yog cov chav kawm. Yog tias tus tsim tsim tau tshaj tawm tias yog tus kheej, ces nws cov khoom tsuas yog siv tau los ntawm hauv chav kawm tshaj tawm. Koj tsis tuaj yeem nkag mus rau nws cov khoom los ntawm sab nraum chav tsim qauv.
Puas yog cov neeg tsim khoom ntiag tug lossis pej xeem?
Nyob hauv C ++, tus tsim tsim yuav raug hu ua thaum khoom ntawm chav kawm raug tsim. Los ntawm lub neej ntawd, cov neeg tsim khoom tau txhais nyob rau hauv public seem ntawm chav kawm.
Vim li cas peb feem ntau tshaj tawm tus tsim ua pej xeem hauv Java?
Public constructor kuj txhais tau tias nws tuaj yeem nkag mus tau sab nraum chav Kawm Lwm chav kuj tuaj yeem tau txais lawv yooj yim txawm li cas los peb ua tus tsim ua ntiag tug nws tsis nkag tau sab nraum chav kawm. Tsis tas li ntawd peb tsim constructor tus constructor raws li pej xeem mus pib chav kawm nyob qhov twg nyob rau hauv qhov kev pab cuam.
Kuv yuav ua li cas thiaj ua rau kuv tus neeg tsim kho pej xeem?
Ua tus tsim pej xeem yog tias koj tso cai rau koj tus neeg siv khoom code sab nraum lub pob instantiate koj cov khoom. Yog koj tsis uaxav tau (vim cov khoom yog pob tshwj xeeb lossis cov khoom nws tus kheej tsis tuaj yeem ncaj qha ncaj qha) siv pob-private.