Puas yog ob yam khoom yuav ib txwm sib npaug thaum lawv qhov piv rau () txoj kev rov xoom?
Puas yog ob yam khoom yuav ib txwm sib npaug thaum lawv qhov piv rau () txoj kev rov xoom?

Video: Puas yog ob yam khoom yuav ib txwm sib npaug thaum lawv qhov piv rau () txoj kev rov xoom?

Video: Puas yog ob yam khoom yuav ib txwm sib npaug thaum lawv qhov piv rau () txoj kev rov xoom?
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Hlis ntuj nqeg
Anonim

Nws yog pom zoo tias piv rau nkaus xwb rov qab los 0, if hu rau sib npaug ntawm tib yam khoom yuav rov qab los muaj tseeb: piv rau (e2) == 0 muaj ib yam boolean tus nqi li e1. sib npaug (e2) rau txhua e1 thiab e2 ntawm chav kawm C. Nco ntsoov tias null yog tsis yog ib qho piv txwv ntawm ib chav kawm, thiab e.

Tsis tas li ntawd, nws puas tuaj yeem sib npaug () rov qab tsis tseeb txawm tias cov ntsiab lus ntawm ob yam khoom zoo ib yam?

4 Teb. Hauv java txoj kev pej xeem boolean sib npaug ( Yam khoom obj) tau txais los ntawm cov Yam khoom . Txawm li cas los xij, kev siv cov txheej txheem raws li tau hais tseg hauv cov Yam khoom class yog hais tias sib npaug txoj kev yuav rov yog tias thiab xwb yog tus ob yam khoom muab piv yog cov ib yam piv txwv.

Tom qab ntawd, lo lus nug yog, vim li cas pivTo () yuav tsum ua raws li qhov sib npaug () txoj hauv kev hauv Java? Qhov no yog li ntawd vim hais tias daim ntawv qhia interface yog txhais nyob rau hauv cov nqe lus ntawm lub sib npaug kev ua haujlwm, tab sis daim ntawv qhia ua txhua qhov kev sib piv tseem ceeb siv nws piv rau (los yog sib piv) txoj kev , yog li ob tus yuam sij uas suav tias yog sib npaug los ntawm qhov no txoj kev yog, los ntawm qhov pom ntawm daim ntawv qhia kev txheeb xyuas, sib npaug.

Ua raws li qhov no, qhov sib txawv ntawm == sib npaug () thiab piv rau () txoj kev yog dab tsi?

piv rau : Sib piv ob txoj hlua lexicographically. sib npaug : Muab cov hlua no sib piv rau cov khoom teev tseg. piv rau piv ob txoj hlua los ntawm lawv cov cim (ntawm tib qhov ntsuas) thiab rov ua tus lej (zoo lossis tsis zoo) raws li. sib npaug () tshawb xyuas yog tias ob yam khoom zoo ib yam lossis tsis yog thiab xa rov qab boolean.

Peb puas tuaj yeem override piv rau txoj kev?

Override sib piv rau Method Nws yuav tsum rov qab tus lej tsis zoo (feem ntau yog -1), yog tias qhov khoom siv tam sim no tsawg dua qhov dhau mus ib , thiab tus lej zoo (feem ntau yog +1) yog ntau dua, thiab 0 yog sib npaug. piv rau txoj kev yuav tsum pov ib qho kev zam yog tias cov khoom dhau los muaj hom incompatible lossis null.

Pom zoo: