Cov txheej txheem:

Yuav ua li cas peb yuav ua kom cov chav kawm tsis hloov hauv Java nrog hnub tim?
Yuav ua li cas peb yuav ua kom cov chav kawm tsis hloov hauv Java nrog hnub tim?

Video: Yuav ua li cas peb yuav ua kom cov chav kawm tsis hloov hauv Java nrog hnub tim?

Video: Yuav ua li cas peb yuav ua kom cov chav kawm tsis hloov hauv Java nrog hnub tim?
Video: Tsis Muaj Peev Xwm Yuav Ua Li Cas 2024, Hlis ntuj nqeg
Anonim

Peb yuav nkag siab tias cov lus qhia no txhais tau li cas los ntawm kev tsim cov chav kawm tsis hloov nrog cov khoom sib hloov nrog Hnub tim

  1. Tsis txhob muab "setter" txoj kev - txoj kev uas hloov teb los yog khoom xa mus los ntawm teb .
  2. Ua tag nrho teb zaum kawg thiab ntiag tug.
  3. Tsis txhob tso cai rau subclasses override txoj kev.

Ntawm no, peb yuav ua li cas thiaj ua tau ib chav kawm tsis hloov hauv Java?

Immutable Class hauv Java

  1. Tshaj tawm cov chav kawm kawg kom nws tsis tuaj yeem txuas ntxiv.
  2. Ua kom txhua qhov chaw ntiag tug kom tsis pub nkag ncaj qha.
  3. Tsis txhob muab cov txheej txheem setter rau variables.
  4. Ua kom tag nrho cov kev hloov pauv tau kawg kom nws tus nqi tuaj yeem raug muab rau ib zaug xwb.
  5. Pib pib tag nrho cov teb ntawm tus tsim qauv ua cov ntawv sib sib zog nqus.

Tom qab ntawd, lo lus nug yog, dab tsi yog kev siv cov chav kawm tsis hloov hauv Java? Chav kawm tsis hloov tau txhais tau hais tias ib zaug ib yam khoom raug tsim, peb hloov tsis tau nws cov ntsiab lus. Hauv Java , tag nrho cov wrapper chav kawm (xws li Integer, Boolean, Byte, Luv) thiab hlua chav kawm yog hloov tsis tau . Peb tuaj yeem tsim peb tus kheej chav kawm tsis hloov tau ib yam nkaus.

Cov neeg kuj nug, yog hnub kawm tsis hloov hauv Java?

Hnub tim tsis yog hloov tsis tau , peb yuav tsum tau ua ib daim ntawv tiv thaiv ntawm java ua . siv. Hnub tim teb thaum xa rov qab siv rau qhov piv txwv no hloov pauv. Cia peb tsim ib tug neeg hypothetical chav kawm uas muaj npe thiab dob tsuas yog ob tug tswv cuab xwb.

Yuav ua li cas peb thiaj ua tau ib yam khoom hloov tsis tau?

Txhawm rau ua kom cov khoom tsis hloov pauv, Koj yuav tsum ua cov kauj ruam no:

  1. Tsis txhob siv ib txoj hauv kev, uas tuaj yeem hloov chaw ntawm koj chav kawm. Piv txwv li tsis txhob siv Setters.
  2. Tsis txhob siv cov pej xeem uas tsis yog qhov kawg. Yog tias koj daim teb yog pej xeem ces koj yuav tsum tshaj tawm lawv li qhov kawg thiab pib lawv hauv tus tsim lossis ncaj qha hauv kab lus tshaj tawm.

Pom zoo: