欢迎来到天天文库
浏览记录
ID:38182732
大小:34.00 KB
页数:3页
时间:2019-05-24
《接口比较两个圆和圆柱的大小》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、比较两个圆和两个圆柱的大小:package圆;//定义包publicinterfaceCircle{publicdoublePI=3.1415;//定义PIpublicintcompearTo(Objecto);}package圆;publicclassCircleimplimplementsCircle{doubler;Circleimpl(doubler){this.r=r;}publicdoublegetAear(){//计算面积returnPI*r*r;}publicintcompear
2、To(Objecto){doublethisAear=getAear();Circleimplobj=(Circleimpl)o;//超类应用变量执行强制类型转换后才能赋予子类量doubleobjArea=obj.getAear();if(thisAear>objArea){return1;}elseif(thisAear==objArea)return0;elsereturn-1;}}package圆;publicclassColumnimplementsCircle{doubler,h;Co
3、lumn(doubler,doubleh){this.r=r;this.h=h;}publicdoublegetArea(){returnr*h*PI*r;}publicintcompearTo(Objecto){doublethisArea=getArea();Columnobj=(Column)o;doubleobjArea=obj.getArea();if(thisArea>objArea)return1;elseif(thisArea==objArea)return0;elseretur
4、n-1;}}package圆;publicclassCircletest{publicstaticvoidmain(String[]args){Circleimplzh1=newCircleimpl(1);Circleimplzh2=newCircleimpl(3);inti=zh1.compearTo(zh2);//比较两个圆面积的大小switch(i){case1:System.out.println("圆1比圆2大");break;case0:System.out.println("圆1等
5、于圆2");break;case-1:System.out.println("圆1小于圆2");break;}Columnzh3=newColumn(3,3);Columnzh4=newColumn(3,3);intj=zh3.compearTo(zh4);switch(j){case1:System.out.println("圆柱1比圆柱2大");break;case0:System.out.println("圆柱1等于圆2柱");break;case-1:System.out.println
6、("圆柱1小于圆2柱");break;}}}
此文档下载收益归作者所有