欢迎来到天天文库
浏览记录
ID:37852846
大小:48.50 KB
页数:6页
时间:2019-06-01
《JAVA抽象类实验代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.抽象类(注:前5个类的主函数没有测试,直接在最后一个测试类中测试的)(1)packagecom.drawing;publicabstractclassShape{abstractvoiddraw();}(2)packagecom.drawing;publicclassPointextendsShape{privateintx;privateinty;publicPoint(intx,inty){super();this.x=x;this.y=y;}publicPoint(){this.x=0;this.y=0;}publicintgetX(){returnx;}publi
2、cvoidsetX(intx){this.x=x;}publicintgetY(){returny;}publicvoidsetY(inty){this.y=y;}publicvoiddraw(){System.out.println("Pointisdrawing!");}publicvoidprint(){System.out.println("x="+x+"y="+y);}publicstaticvoidmain(String[]args){}}(3)packagecom.drawing;publicclassLineextendsShape{privatePointp
3、Start=null;privatePointpEnd=null;publicLine(PointpStart,PointpEnd){super();this.pStart=pStart;this.pEnd=pEnd;}publicLine(){super();}publicPointgetpStart(){returnpStart;}publicvoidsetpStart(PointpStart){this.pStart=pStart;}publicPointgetpEnd(){returnpEnd;}publicvoidsetpEnd(PointpEnd){this.pE
4、nd=pEnd;}publicStringtoString(){return"Line:"+pStart.toString()+pEnd.toString();}publicvoiddraw(){System.out.println("Lineisdrawing!");}publicvoidprint(){System.out.println("pStart:"+pStart.getX()+","+pStart.getY()+";pEnd:"+pEnd.getX()+","+pEnd.getY());}publicbooleanequals(Objecto){if(this=
5、=o)returntrue;if(o==null)returnfalse;if(o.getClass()!=this.getClass())returnfalse;else{Linel=(Line)o;if(l.pStart==this.pStart&&l.pEnd==this.pEnd){returntrue;}elsereturnfalse;}}publicdoubledistance(){returnMath.sqrt((pStart.getX()-pEnd.getX())*(pStart.getX()-pEnd.getX())+(pStart.getY()-pEnd.
6、getY())*(pStart.getY()-pEnd.getY()));}publicstaticvoidmain(String[]args){}}(4)packagecom.drawing;publicclassCircleextendsShape{privatePointcenter=null;privatedoubleradius=0;publicCircle(Pointcenter,doubleradius){super();this.center=center;this.radius=radius;}publicCircle(){super();}publicvo
7、iddraw(){System.out.println("Centerisdrawing!");}publicStringtoString(){return"center:"+"("+center.getX()+","+center.getY()+")"+","+"radius:"+radius;}publicvoidprint(){System.out.println("centeris"+"("+center.getX()+","+center.getY()+")"+";"+"radiusis:"+
此文档下载收益归作者所有