范型攻略篇物业

铸造及热处理2021年09月06日

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.

classCoordsTextendsTwoD{

T[]coords;

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

}

//Demonstrateaboundedwildcard.

publicclassBoundedWildcard{

staticvoidshowXY(Coords?c){

intln(\"XYCoordinates:\");

for(inti=0;ngth;i++)

intln(ords[i].x+\"\"+

ords[i].y);

intln();

}

staticvoidshowXYZ(Coords?extendsThreeDc){

intln(\"XYZCoordinates:\");

for(inti=0;ngth;i++)

intln(ords[i].x+\"\"+

ords[i].y+\"\"+

ords[i].z);

intln();

}

staticvoidshowAll(Coords?extendsFourDc){

intln(\"XYZTCoordinates:\");

for(inti=0;ngth;i++)

intln(ords[i].x+\"\"+

ords[i].y+\"\"+

ords[i].z+\"\"+

ords[i].t);

intln();

}

publicstaticvoidmain(Stringargs[]){

TwoDtd[]={

newTwoD(0,0),

newTwoD(7,9),

newTwoD(18,4),

newTwoD(-1,-23)

};

CoordsTwoDtdlocs=newCoordsTwoD(td);

intln(\"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)

};

CoordsFourDfdlocs=newCoordsFourD(fd);

intln(\"Contentsoffdlocs.\");

//TheseareallOK.

showXY(fdlocs);

showXYZ(fdlocs);

showAll(fdlocs);

}

}

rayList的Generics

//Example16

publicclassArrayListGenericDemo{

publicstaticvoidmain(String[]args){

ArrayListStringdata=newArrayListString();

d(\"hello\");

d(\"goodbye\");

//d(newDate());Thiswon\'tcompile!

IteratorStringit=erator();

while(sNext()){

Strings=xt();

intln(s);

}

}

}

shMap的Generics

//Example17

publicclassHashDemoGeneric{

publicstaticvoidmain(String[]args){

HashMapInteger,Stringmap=newHashMapInteger,String();

t(1,\"Ian\");

t(42,\"Scott\");

t(123,\"Somebodyelse\");

Stringname=t(42);

intln(name);

}

}

8.接口的Generics//Example18

interfaceMinMaxTextendsComparableT{

Tmin();

Tmax();

}

//Now,implementMinMax

classMyClassTextendsComparableTimplementsMinMaxT{

T[]vals;

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

//Returntheminimumvalueinvals.

publicTmin(){

Tv=vals[0];

for(inti=1;ngth;i++)

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

returnv;

}

//Returnthemaximumvalueinvals.

publicTmax(){

Tv=vals[0];

for(inti=1;ngth;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\'};

MyClassIntegeriob=newMyClassInteger(inums);

MyClassCharactercob=newMyClassCharacter(chs);

intln(\"Maxvalueininums:\"+x());

intln(\"Minvalueininums:\"+n());

intln(\"Maxvalueinchs:\"+x());

intln(\"Minvalueinchs:\"+n());

}

但玩家却能在狭小的空间中逗留更久 }

ception的Generics

//Example20

interfaceExecutorEextendsException{

voidexecute()throwsE;

}

publicclassGenericExceptionTest{

publicstaticvoidmain(Stringargs[]){

try{

ExecutorIOExceptione=

newExecutorIOException(){

publicvoidexecute()throwsIOException

{

//codeherethatmaythrowan

//IOExceptionorasubtypeof

//IOException

}

};

ecute();

}catch(IOExceptionioe){

intln(\"IOException:\"+ioe);

intStackTrace();

}

}

}

百色白癜风医院哪家好
福州治疗早泄费用
防城港哪里治疗白癜风效果好
相关阅读
遵义第一波消费券几分钟内全部抢完!未来三周还有三波

封面新闻业 易弋力 “奔到唯600元减200元的大额券去的,最后抢夺了唯50元减...

2023-10-13
千万不要随便穿著肉色的衣服…哈哈哈哈狠狠代入了!

以前的宝子们 有不会一如既往地持续保持好心情呢 如果不会也就让 以前的开...

2023-09-26
洒下保险业抗疫“及时雨”,上海银行业如何为中小企业注入活水?

图片来源不明:视觉里面国 电视新闻记者 | 孙艺真 张晓云 流感封控以来,...

2023-09-25
券业重磅!行业诚信准则发布,声誉信息硬指标明确,失信开销大大增强

证券产品从业其他部门看刚才,产业忠信原则上、大律师信誉反馈比赛规则发...

2023-09-22
生来双目皆盲,却因修炼奇术成了“天眼大仙”不料看错姻缘,厄运连累整个后代

诱骗旺妻互为 清乾隆年有数,近城有个寡人,叫孙闻;大。此人有一段多种...

2023-09-20
美国资本集团减持康希诺脊椎动物-B(06185)61.12万股 每股作价约80.63港元

香港联交所近期资料显示,7月4日,英美两国企业集团增资康希诺生物体-B(06...

2023-09-18
友情链接