欢迎来到天天文库
浏览记录
ID:47736613
大小:46.50 KB
页数:9页
时间:2020-01-16
《Java面向对象经典案例10个》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、1classAnthropoid//类人猿{privateintn=100;voidcrySpeak(Strings){System.out.println(s);}}classPeopleextendsAnthropoid{voidcomputer(inta,intb){intc=a*b;System.out.println(c);}voidcrySpeak(Strings){System.out.println("**"+s+"**");}}publicclassMonkey{publicstaticvoidmain(Stringargs
2、[]){Anthropoidmonkey=newPeople();//monkey是People对象的上转型对象//monkey.computer(10,10);//非法monkey.crySpeak("我喜欢这个运动");Peoplepeople=(People)monkey;//把上转型对象强制转化为子类的对象people.computer(10,10);}}2classManyArea{publicdoublearea(doubleradius){returnMath.PI*radius*radius;}publicdoublearea
3、(doublelen,doublewidth){returnlen*width;}publicdoublearea(intlen,intwidth){returnlen*width;}publicdoublearea(doublelen,doublewidth,doubleheight){returnlen*width*height;}}publicclassOverLoad{publicstaticvoidmain(Stringargs[]){ManyAreaob=newManyArea();System.out.println("半径为3
4、.0的圆的面积:"+ob.area(3.0));System.out.println("长2.0、宽3.0的面积:"+ob.area(2.0,3.0));System.out.println("长2、宽3的面积:"+ob.area(2,3));System.out.println("立方体的面积:"+ob.area(2.0,3.0,4.0));}}3classAnimal{publicvoidshout(){}}classDogextendsAnimal{publicvoidnewDog(){System.out.println("Dog的新
5、特性");}publicvoidshout(){System.out.println("汪");}}classCatextendsAnimal{publicvoidshout(){System.out.println("喵");}}classTest{publicvoidanimalshout(Animala){a.shout();}}publicclassPolyEx{publicstaticvoidmain(String[]args){Animald=newDog();//(1)Dogd1=(Dog)d;//(3)父类对象强制转换成子类对
6、象d1.newDog();//d.newDog();d.shout();Testt=newTest();t.animalshout(d);//(2)t.animalshout(d1);}}4classArrayEx{publicint[]subarray(inta[],intstart,intend){intsubarr[]=newint[end-start];for(inti=0,j=start;j7、cvoidmain(Stringargs[]){ArrayExarrex=newArrayEx();intarr[]=newint[10];for(inti=0;i8、h=wid;height=hei;}}classShowBox{voidshow(Boxb){System.out.println(b.length+""+b.wi
7、cvoidmain(Stringargs[]){ArrayExarrex=newArrayEx();intarr[]=newint[10];for(inti=0;i8、h=wid;height=hei;}}classShowBox{voidshow(Boxb){System.out.println(b.length+""+b.wi
8、h=wid;height=hei;}}classShowBox{voidshow(Boxb){System.out.println(b.length+""+b.wi
此文档下载收益归作者所有