搏击

Java 范型攻略篇3

2019-09-13 20:35:52来源:励志吧0次阅读

5.使用边界通配符//Example15

classTwoD{

intx,y;

TwoD(inta,intb){

x=a;

y=b;

}

}

//Three-dimensionalcoordinates.

classThreeDextendsTwoD{

intz;

ThreeD(inta,intb,intc){

super(a,b);

z=c;

}

}

//Four-dimensionalcoordinates.

classFourDextendsThreeD{

intt;

FourD(inta,intb,intc,intd){

super(a,b,c);

t=d;

}

}

//Thisclassholdsanarrayofcoordinateobjects.

classCoords<TextendsTwoD>{

T[]coords;

Coords(T[]o){coords=o;}

}

//Demonstrateaboundedwildcard.

publicclassBoundedWildcard{

staticvoidshowXY(Coords<?>c){

System.out.println("XYCoordinates:");

for(inti=0;i<c.coords.length;i++)

System.out.println(c.coords[i].x+""+

c.coords[i].y);

System.out.println();

}

staticvoidshowXYZ(Coords<?extendsThreeD>c){

System.out.println("XYZCoordinates:");

for(inti=0;i<c.coords.length;i++)

System.out.println(c.coords[i].x+""+

c.coords[i].y+""+

c.coords[i].z);

System.out.println();

}

staticvoidshowAll(Coords<?extendsFourD>c){

System.out.println("XYZTCoordinates:");

for(inti=0;i<c.coords.length;i++)

System.out.println(c.coords[i].x+""+

c.coords[i].y+""+

c.coords[i].z+""+

c.coords[i].t);

System.out.println();

}

publicstaticvoidmain(Stringargs[]){

TwoDtd[]={

newTwoD(0,0),

newTwoD(7,9),

newTwoD(18,4),

newTwoD(-1,-23)

};

Coords<TwoD>tdlocs=newCoords<TwoD>(td);

System.out.println("Contentsoftdlocs.");

showXY(tdlocs);//OK,isaTwoD

//showXYZ(tdlocs);//Error,notaThreeD

//showAll(tdlocs);//Erorr,notaFourD

//Now,createsomeFourDobjects.

FourDfd[]={

newFourD(1,2,3,4),

newFourD(6,8,14,8),

newFourD(22,9,4,9),

newFourD(3,-2,-23,17)

};

Coords<FourD>fdlocs=newCoords<FourD>(fd);

System.out.println("Contentsoffdlocs.");

//TheseareallOK.

showXY(fdlocs);

showXYZ(fdlocs);

showAll(fdlocs);

}

}

6.ArrayList的Generics

//Example16

publicclassArrayListGenericDemo{

publicstaticvoidmain(String[]args){

ArrayList<String>data=newArrayList<String>();

data.add("hello");

data.add("goodbye");

//data.add(newDate());Thiswon'tcompile!

Iterator<String>it=data.iterator();

while(it.hasNext()){

Strings=it.next();

System.out.println(s);

}

}

}

7.HashMap的Generics

//Example17

publicclassHashDemoGeneric{

publicstaticvoidmain(String[]args){

HashMap<Integer,String>map=newHashMap<Integer,String>();

map.put(1,"Ian");

map.put(42,"Scott");

map.put(123,"Somebodyelse");

Stringname=map.get(42);

System.out.println(name);

}

}

8.接口的Generics//Example18

interfaceMinMax<TextendsComparable<T>>{

Tmin();

Tmax();

}

//Now,implementMinMax

classMyClass<TextendsComparable<T>>implementsMinMax<T>{

T[]vals;

MyClass(T[]o){vals=o;}

//Returntheminimumvalueinvals.

publicTmin(){

Tv=vals[0];

for(inti=1;i<vals.length;i++)

if(vals[i].compareTo(v)<0)v=vals[i];

returnv;

}

//Returnthemaximumvalueinvals.

publicTmax(){

Tv=vals[0];

for(inti=1;i<vals.length;i++)

if(vals[i].compareTo(v)>0)v=vals[i];

returnv;

}

}

publicclassGenIFDemo{

publicstaticvoidmain(Stringargs[]){

Integerinums[]={3,6,2,8,6};

Characterchs[]={'b','r','p','w'};

MyClass<Integer>iob=newMyClass<Integer>(inums);

MyClass<Character>cob=newMyClass<Character>(chs);

System.out.println("Maxvalueininums:"+iob.max());

System.out.println("Minvalueininums:"+iob.min());

System.out.println("Maxvalueinchs:"+cob.max());

System.out.println("Minvalueinchs:"+cob.min());

}

}

9.Exception的Generics

//Example20

interfaceExecutor<EextendsException>{

voidexecute()throwsE;

}

publicclassGenericExceptionTest{

publicstaticvoidmain(Stringargs[]){

try{

Executor<IOException>e=

newExecutor<IOException>(){

publicvoidexecute()throwsIOException

{

//codeherethatmaythrowan

//IOExceptionorasubtypeof

//IOException

}

};

e.execute();

}catch(IOExceptionioe){

System.out.println("IOException:"+ioe);

ioe.printStackTrace();

}

}

}

三岁宝宝不爱吃饭是什么原因
威门热淋清颗粒有用吗
动脉硬化吃什么药
剖宫产术后护理措施有哪些
分享到: