Java Generics Dzidziso - Ndeapi maGeneral uye Maitiro Ekushandisa Iwo?

Java Generics ndechimwe chezvinhu zvakakosha zvemutauro weJava. Pfungwa iri kumashure kwemajeniki iri nyore, zvisinei, dzimwe nguva inouya seyakaomarara nekuda kwekuchinja kubva kune yakajairika syntax inosangana nayo.

Chinangwa chedzidziso iyi ndechekukuzivisa iwe kune ino inobatsira pfungwa yeijeniki nenzira iri nyore kunzwisisa.

Asi tisati tanyura mumagetsi chaiwo, ngationei kuti nei majekiseni e Java aidiwa pakutanga.

Chinangwa cheJava Generics

Usati waunzwa majenareta mu Java 5, unogona kunyora nekunyora kodhi snippet seizvi pasina kukanda chikanganiso kana yambiro:

List list = new ArrayList(); list.add('hey'); list.add(new Object());

Iwe unogona kuwedzera kukosha kwemhando ipi neipi kune chinyorwa kana imwe Java Unganidzo pasina kuzivisa kuti ndeipi dhata yainochengeta. Asi kana iwe uchinge watora hunhu kubva pane iyo runyorwa, iwe unofanirwa kuikanda zvakajeka kune imwe mhando.

Funga nezve iterating kuburikidza neiri pamusoro runyorwa.



for (int i=0; i< list.size(); i++) {
String value = (String) list.get(i); //CastClassException when i=1 }

Kubvumidza kugadzirwa kwechinyorwa pasina kutanga kuzivisa iyo yakachengetwa dhata dhata, sezvatakaita, zvinogona kukonzera programmers kuita zvikanganiso senge pamusoro izvo zvinokanda ClassCastExceptions panguva yekumhanya.

Zvigadzirwa zvakaunzwa kudzivirira ivo programmers kubva mukukanganisa kwakadaro.

Nemagetsi, unogona kunyatso kuzivisa mhando yedata iri kuzochengetwa paunenge uchigadzira Java Unganidzo seyinotevera muenzaniso inoratidza.

Ongorora:Iwe unogona achiri kugadzira Java yekuunganidza chinhu pasina kudoma yakachengetwa dhata mhando asi haina kukurudzirwa. List stringList = new ArrayList();

Zvino, haugone nekukanganisa kuchengeta iyo Integer mune yeTambo mhando runyorwa pasina kukanda yekuunganidza-nguva kukanganisa. Izvi zvinoona kuti chirongwa chako hachimhanye mukukanganisa nguva yekumhanya.

stringList.add(new Integer(4)); //Compile time Error

Chinangwa chikuru chekupinzwa kwemajeniki kuJava kwaive kudzivirira kumhanyisa mu ClassCastExceptions panguva yekumhanya.

Kugadzira Java Generics

Unogona kushandisa zvigadzirwa kugadzira Java makirasi uye nzira. Ngatitarisei pane iyo mienzaniso yekuti tingaite sei mageneral emhando imwe neimwe.

Chikoro cheGeneric

Paunenge uchigadzira generic kirasi, iyo mhando paramende yekirasi inowedzerwa kumagumo ezita rekirasi mukati mekona mabhureki.

public class GenericClass {
private T item;
public void setItem(T item) {
this.item = item;
}
public T getItem() {
return this.item;
} }

Pano, T ndiyo dhata yemhando yedata. T, N, uye E mamwe emamwe mavara anoshandiswa kudhata yemhando yedhata zvinoenderana nemisangano yeJava.

Mumuenzaniso uri pamusoro, iwe unogona kuupfuudza iyo yakasarudzika dhata mhando paunenge uchigadzira iyo GenericClass chinhu.

public static void main(String[] args) {
GenericClass gc1 = new GenericClass();
gc1.setItem('hello');
String item1 = gc1.getItem(); // 'hello'
gc1.setItem(new Object()); //Error
GenericClass gc2 = new GenericClass();
gc2.setItem(new Integer(1));
Integer item2 = gc2.getItem(); // 1
gc2.setItem('hello'); //Error }

Iwe haugone kupfuudza yechinyakare dhata dhata kune iyo data mhando paramende paunenge uchigadzira generic kirasi chinhu. Dhata chete mhando dzinowedzera Chinhu chinhu ndidzo dzinogona kupfuudzwa semhando paramita.

Semuyenzaniso:

GenericClass gc3 = new GenericClass(); //Error

Maitiro Akajairika

Kugadzira generic nzira zvinotevera maitiro akafanana ekugadzira generic makirasi. Iwe unogona kushandisa nzira yakajairika mukati mekirasi generic pamwe neiyo isiri generic.

public class GenericMethodClass {
public static void printItems(T[] arr){
for (int i=0; i< arr.length; i++) {

System.out.println(arr[i]);
}
}
public static void main(String[] args) {
String[] arr1 = {'Cat', 'Dog', 'Mouse'};
Integer[] arr2 = {1, 2, 3};

GenericMethodClass.printItems(arr1); // 'Cat', 'Dog', 'Mouse'
GenericMethodClass.printItems(arr2); // 1, 2, 3
} }

Pano, iwe unogona kupfuudza rondedzero yerudzi rwakati kuti paramende nzira yacho. Maitiro akajairwa PrintItems() iterates kuburikidza neakapfuura akarongeka uye anodhinda zvinhu zvakachengetwa senge zvakajairwa Java nzira.

Yakatemerwa Rudzi Paramende

Parizvino, akajairwa makirasi uye nzira dzatakagadzira pamusoro dzinogona kuiswa paramende kune chero dhata mhando kunze kwemhando dzekare. Asi ko kana isu tichida kudzikamisa mhando dzedhata dzinogona kupfuudzwa kumagetsi? Apa ndipo panosungwa mhando parameter inouya.

Iwe unogona kusunga mhando dzedhata dzinogamuchirwa nekirasi generic kana nzira nekutsanangudza kuti inofanirwa kunge iri chikamu cheimwe mhando yedata.

Semuyenzaniso:

//accepts only subclasses of List public class UpperBoundedClass{
//accepts only subclasses of List
public void UpperBoundedMethod(T[] arr) {
} }

Pano, iyo UpperBoundedClass uye UpperBoundedMethod inogona kungoiswa paramende uchishandisa zvidiki zve List Rudzi rwe data.

List dhata mhando inoshanda senge yepamusoro yakasungirirwa kune iyo mhando paramende. Kana iwe ukaedza kushandisa dhata mhando isiri subtype ye List, iyo inokanda yekuunganidza-nguva kukanganisa.

Iyo miganho haina kuganhurirwa kumakirasi chete. Iwe unokwanisa kupfuudza zvinongedzo futi. Kuwedzeredza chinongedzo zvinoreva, mune ino kesi, kushandisa interface.

Iyo paramende inogona zvakare kuve nemipanda yakawanda seiyi muenzaniso inoratidza.

//accepts only subclasses of both Mammal and Animal public class MultipleBoundedClass{
//accepts only subclasses of both Mammal and Animal
public void MultipleBoundedMethod(T[] arr){
} }

Rudzi rwekugamuchira dhata runofanirwa kuve chikamu chevose veMhuka neMammal makirasi. Kana mumwe weiyi mitsara iri kirasi, inofanira kuuya pakutanga mune yakasungwa kuzivisa.

Mumuenzaniso wepamusoro, kana Mammal iri kirasi uye Mhuka iri interface, Mammal anofanira kuuya kutanga sezvakaratidzwa pamusoro. Zvikasadaro, kodhi yacho inokanda yekukanganisa-nguva kukanganisa.

Java Generics Makadhi emusango

Makadhi emusango anoshandiswa kupfuudza paramende zvemhando dzakajairika kunzira. Kusiyana neyakajairika nzira, pano, generic paramende inopfuudzwa kumatanho anotambirwa nenzira, iyo yakasiyana nedata remhando paramende yatakurukura pamusoro. Kadhi yemusango inomiririrwa ne? chiratidzo.

public void printItems(List list) {
for (int i=0; i< list.size(); i++) {
System.out.println(list.get(i));
} }

Izvo zviri pamusoro printItems() nzira inogamuchira zvinyorwa zvemhando ipi neipi yedata sedanho. Izvi zvinodzivirira programmers kubva mukufanirwa kudzokorora makodhi ezvinyorwa zvemhando dzakasiyana dzemhando, izvo zvingave zvakadaro pasina majeniki.

Kumusoro Kwakavhenganiswa Masango

Kana isu tichida kudzikisira mhando dzedata dzakachengetwa mune rondedzero inogamuchirwa nenzira, tinogona kushandisa yakasungwa wildcards.

Muenzaniso:

public void printSubTypes(List list) {
for (int i=0; i< list.size(); i++) {
System.out.println(list.get(i));
} }

printSubTypes() nzira inogamuchira chete zvinyorwa zvinochengeta zvidiki zveMuvara. Iyo inogamuchira runyorwa rweRedColor kana BlueColor zvinhu, asi haigamuchire runyorwa rweZvipfuyo zvinhu. Izvi zvinodaro nekuti Mhuka haisi subtype yeMuvara. Uyu muenzaniso weiyo yepamusoro-yakasungwa yemusango kadhi.

Dzakadzika Bounded Wildcards

Saizvozvo, dai isu taive:

public void printSuperTypes(List list) {
for (int i=0; i< list.size(); i++) {
System.out.println(list.get(i));
} }

ipapo, iyo _ _ _ _ | nzira chete inogamuchira zvinyorwa zvinochengetera mhando dzakanakisa dzembwa kirasi. Inotambira runyorwa rweMammal kana Animal zvinhu asi kwete runyorwa rweLabDog zvinhu nekuti LabDog haisi superclass yeImbwa, asi subclass. Uyu muenzaniso weiyo yepasi-yakasungwa kadhi yemusango.

Mhedziso

Java Generics chave chinhu icho programmers vasingakwanise kurarama vasina kubva kubvira kwayakatanga.

Uku kufarirwa kunokonzerwa nemhedzisiro yaro pakuita kuti mapurogiramu ehupenyu ave nyore. Zvimwe pane kuvadzivirira kubva mukukanganisa kukodha, iko kushandiswa kwemajeniki kunoita kuti kodhi isadzokorore. Wakaona here kuti inogadzira sei makirasi uye nzira dzekudzivirira kuve nekudzokorora kodhi yemhando dzakasiyana dzedata?

Kuve nekunzwisisa kwakanaka kwemageniki kwakakosha kuve nyanzvi mumutauro. Saka, kushandisa zvawakadzidza mune ino dzidziso mune inoshanda kodhi ndiyo nzira yekuenda kumberi izvozvi.