Cov kauj ruam yuav ua hauj lwm nrog Java generics?

Java Generics

Taw qhia rau Java Generics

Txheej txheem cej luam:

Tus tshiab version Java 8 SEA tau tuav lub ntiaj teb los daus los te loj. Nws boasts ntawm nws cov tshiab programming peev xwm thiab nta. Zuag qhia tag nrho, Nws leg ntaubntawv kom nws ua tau yuav txo lub chaw ua hauj lwm ntawm nws cov neeg siv thiab txhim khu cov kev coding heev. Txawm li cas los, thiaj li to taub txog nws cov nta, ib tug yuav tsum paub txog cov generics. Feem ntau, Ib lub caijnyoog twg uas yuav tsum tau mus xyuas qhov tseem ceeb uas koj yuav tau ntxiv rau koj ArrayList thiab Collections thiab tseem xav tau tej yam khoom nyob rau hauv koj cov khoom ArrayList. Koj yuav siv tau cov Generics no Java. Generics kuj pub tus neeg siv yuav xyuas seb cov Comparator siv cov khoom uas tsim nyog siv los yog tsis. No muaj nuj nqi no yog ib yam dab tsi compile, Ces tsuas sau thaum compiling qhov chaws, akin to C templates.

Nyob rau cov tshooj no, Peb yuav paub ntau ntxiv txog qhov tseem ceeb ntawm cov generics thiab yuav kawm li cas los kov lawv.








Vim li cas cov generics yog ib qho tseem ceeb?

Generics yog ib qho tseem ceeb heev rau cov kev txiav txim zoo uas lawv tso cai rau tej yam khoom los txhais cov hoob kawm. Yog li, Generics yuav siv tau los rov siv cov txuj lom tsis txaus ntshai. Generic code muaj ntau cov kev pab dua tsis-generic ones. Lawv pab sai thiab tom qab qhov nyiaj no muaj tsawg kawg uas yooj yim thiab exceptions tshwm sim hauv daim ntawv Thov Java. Qee yam kev pab uas siv cov generics hauv Java no muab hauv qab no.

  • Zoo dua-khij Thaum cov chaws no yog deployed: Yog tshwm sim nyob rau hauv ib qho chaws uas tsis yog-generic code, Nws yuav nyuaj heev rau daws thaum nws yuav muaj ib tug runtime-yuam kev. Runtime yuam kev yog ib qho nyuaj heev rau koj nrhiav thiab daws. Rau lwm cov tes, yog ib tug generic code compiled, Feem ntau yuav muaj compile-time khij yuam kev ua tus Java compiler nruj me ntsis cov tshev mis nruj me ntsis checks generic codes rau hom yuam kev. Compile-time yooj yim npaum li cas los daws piv rau khiav yuam kev. Nws kuj txuag lub sij hawm los ntawm kev tsis zoo li ClassCastException uas yog khiav yuam kev.
  • Tsis muaj kev nruab nrab: Casting yog ib tug yuav tsum tau nyob rau hauv cov chaws uas tsis yog-generic code uas tso cai rau lub compiler Java compiler paub txog cov khoom thiab lawv hom nyob rau hauv ib phau ArrayList los yog ib phau ntawv sau. Incorrect txhais hais tias yuav xa yuam kev xws li ClassCastException uas yog khiav yuam kev. Cov yuam kev no yuav nyuaj heev los daws tau raws li tus tsim tawm yuav tau los ntawm qhov chaws tseem thiaj li nrhiav tau qhov erratic casting. Rau lwm cov tes, generics kiag li tsis sib haum no ua kev hem thawj raws li nws tsis tau casting hlo. Hom kwv yog twb paub los ntawm lub compiler, Li ntawd, tus code tsis tau cais casting. Qhov no tsis tsuas nres feem ntau ntawm cov runtime yuam kev, Tab sis kuj txuag lub sij hawm uas yuav txwv tsis pub tau thaum sau ntawv siv casting codes.

  • Generic algorithms yuav tsim: Generics nyob Java pub rau cov developers siv los siv ib yam algorithm hu ua ib generic generic algorithm. Generic algorithms cia tus developers sai tsim collections txawv hom, thaum uas tsis kam ua. Qhov no pub sai coding uas tseem yog thiab muaj kev ruaj ntseg zoo. Tseem, Tus generic generic algorithm yuav tau hloov raws li cov lus qhia thiab yuav tsum tau ua daim ntawv thov thiab tus tsim tawm.
  • Generics yuav siv tau rau txoj kev loj hlob ntawm tshwj xeeb Java muaj. Suppose koj xav tsim ib lub thawv uas yuav siv tau los mus thauj khoom ntim rau hauv daim ntawv thov. Suab yooj yim txaus? Tiam sis yog ib tug ntes. Muaj ntau hom khoom, neb ib leeg twg muaj ntau hom thiab siv. Li ntawd, lub thawv uas koj tsim nyog ua kom haum txhua hom khoom. Muaj ntau yam kev tov rau qhov no. Koj yuav tsim ib lub thawv uas yuav khaws cov hom khoom thiab nrum nws. Txawm li cas los, Qhov no muaj feem ntau yuav siv tau, Thaum nws yuav ua tau ntau yam ntxiv thaum khiav vim nws yam-unsafe xwm. Generics yuav siv tau no, thiaj li yuav tshem qhov kev txwv no yog hloov cov hom generic. Qhov no yuav ua rau lub thawv uas muaj kev ruaj ntseg thiab yuav tshem tej yam uas yuav muaj tau ntxiv. Tej kev siv tej generics.

Yuav tsum nyeem – Nyiam tsab xov xwm ntawm Java

Kev kawm – Cov neeg ua hauj lwm generics li cas?

Java muaj ib co sau hoob kawm, piv txwv li ArrayList, HashMap thiab TreeList. Cov hoob kawm muaj cov hom phiaj zoo ib yam, kuj hu ua generic khoom. Dhau, ua ntej yuav siv cov generics, yam khoom yuav tau cia rau hauv ib hoob kawm thiab lub compiler yuav tsis ceeb toom txog yam khoom. Txawm li cas los, Java 1.5 nkag tau rau hauv tsev, uas txhais tau tias hom khoom yuav tsum tau txhais hais tias yuav tsum tau txhais hais tias yuav tiv thaiv loopholes hauv lub Tshuag Virtual, Uas tau raug exploitation maliciously.

Generics tau tiag DJK 5. Nws txhais tau tias ib hoob kawm generic muaj cov khoom uas tsuas ib hom. Qhov no meant tias casting twb tsis ntev yuav tsum tau thiab qhov chaws tseem yog sau raws li ib hom uas yuav piav rau lub compiler thaum compiling qhov kev pab cuam. Ua li no, generics ua hauj lwm hauv Java 5 thiab hav.

Teeb ib puag ncig:

Yuav kom paub meej tias qhov chaw raws li cov kauj ruam nram qab no yog cov kauj ruam nram qab no

  • Download tau JDK ntawm qhov link hauv qab no thiab muab ib txog kev faib chav kawm ntawv

http://www.oracle.com/technetwork/java/javase/downloads/index.html

Tam sim no koj ib puag ncig yog npaj sau generic classes hauv Java. Nyob rau sab tom ntej no peb yuav tsim ib co qauv siv.

Coj daim ntawv:

Hauv qhov ua piv txwv, Peb yuav sau ib hoob kawm generic uas yuav noj integer thiab hlua input. Thiab ces nws yuav sau cov zis.

Qhia 1: Generic class piv txwv

[chaws]

pob khoom.eduonix;

Pej xeem hoob kawm GenericClassDemo<G> {

kheej G;

pej xeem tsis txhob ntxiv(G) {

qhov no.g = g = g;

}

Pej xeem GVal() {

rov qab los;

}

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) {

//Tsim integer thiab hlua ntau yam

GenericClassemo<Integer> Intengen = GenericClassDemo<Integer>();

GenericClassemo<Txoj hlua> stringGen = GenericClassDemo GenericClassemo<Txoj hlua>();

//Ntxiv integer thiab txoj hlua ntawm qhov tseem ceeb

Intgen.adval(Integer tshiab(555));

sawv ntxov.adval(hlua tshiab(“Txais tos rau Eduonix”));

//Printing integer thiab txoj hlua ntawm qhov tseem ceeb

System.out.println(“Wb kos tus nqi integer :%d”, intgen.getVal());

System.out.println(“Wb mus xyuas tus nqi hlua :%sab nraud”, sawv ntxov.taug kev...());

}

}

[/chaws]

Cov zis yuav tau ua raws li muaj nyob rau hauv cov tshuaj tivthaiv hauv qab no.

Java Generics

Java Generics

Duab 1: Tso zis

Xaus:

Generic yog ib feem tseem ceeb ntawm Java. Hnub no, Ntau yam nta uas yog ib staple hauv Java siv vim qhov no ib zaug xwb mechanism. Nws pub sai thiab npaum coding thiab tseem negates loopholes thiab kev ruaj ntseg. Thaum tsab xov xwm no hais txog cov fundamental ntawm cov generics, Generics yuav to taub nyob rau hauv ib txoj kev zoo npaum li cas yog hais tias tus tsim tawm ua tau coding.







============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share