欢迎来到天天文库
浏览记录
ID:57275803
大小:21.00 KB
页数:3页
时间:2020-08-08
《定义一个抽象基类Shape.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、//定义一个抽象基类Shape,其中包含area方法abstractclassShape{publicabstractdoublearea();}//定义Cirele并且继承Shape类,实现area方法classCircleextendsShape{privatedoubler;publicCircle(doubler){this.r=r;}publicdoublearea(){returnMath.PI*r*r;}}//定义Rectangle并且继承Shape类,实现area方法,并且对其进行重载classRectangleextendsShape{private
2、doubler;privatedoublelength;publicRectangle(doubler,doublelength){this.r=r;this.length=length;}publicdoublearea(){doubletemp;temp=2*Math.PI*r*length;returntemp+2*Math.PI*r*r;}publicdoublearea(doubler,doublelength){doubletemp;temp=2*Math.PI*r*length;returntemp+2*Math.PI*r*r;}}//定义MyTest作
3、为测试类publicclassMyTest{publicstaticvoidmain(String[]args){Circled=newCircle(2);Rectangles=newRectangle(2,2);System.out.println(d.area());System.out.println(s.area());System.out.println(s.area(3,3));}}importjava.util.*;classMydate{privateCalendardate=Calendar.getInstance();publicvoidsetTi
4、me(inthour,intminute,intsecond){Datetemp=newDate();Temp.sethous(hour);Temp.setminutes(minute);Temp.setseconds(second);date.setTime(temp);}publicvoidsetDate(intyear,intmonth,intday){date.set(year,month-1,day);}publicStringgetNowDate(){returndate.get(Calendar.YEAR)+"年"+(date.get(Calendar.
5、MONTH)+1)+"月"+date.get(Calendar.DAY_OF_MONTH)+"日";}publicStringgetNowTime(){returndate.get(Calendar.HOUR)+":"+date.get(Calendar.MINUTE)+":"+date.get(Calendar.SECOND);}}publicclassTest{publicstaticvoidmain(String[]args){Mydatemy=newMydate();System.out.println(my.getNowDate());System.out.
6、println(my.getNowTime());my.setDate(2012,9,9);my.setTime(1,12,12);System.out.println(my.getNowDate());System.out.println(my.getNowTime());}}
此文档下载收益归作者所有