欢迎来到天天文库
浏览记录
ID:50827002
大小:36.45 KB
页数:2页
时间:2020-03-15
《JAVA继承类求几何图形面积周长代码.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、classshap2D{staticdoublegetArea;//面积staticdoublegetPerimeter;//周长}classcircleextendsshap2D{doubler;doublegetRadius(){returnr=2;}doublegetArea(){returnMath.PI*r*r;}doublegetPerimeter(){return2*Math.PI*r;}publicStringtoString(){Strings;returns="周长:"+getPerimeter()+"面积:"
2、+getArea();}}classTriangleextendscircle{doublea,b,c;Triangle(doublex,doubley,doublez){a=x;b=y;c=z;}voidsetA(doublex){a=x;}doublegetA(){returna;}publicdoublegetPerimeter(){returna+b+c;}publicdoublegetArea(){doubles=(a+b+c)/2;returnMath.sqrt(s*(s-a)*(s-b)*(s-c));}public
3、booleanisTriangle(){booleanflag=false;if(a>0&&b>0&&c>0&&a+b>c&&a+c>b&&c+b>a)flag=true;returnflag;}}classrectangleextendscircle{doublea,b;rectangle(doublex,doubley){a=x;b=y;}voidsetA(doublex){a=x;}doublegetA(){returna;}publicdoublegetPerimeter(){return2*(a+b);}publicdo
4、ublegetArea(){returna*b;}}publicclassShap{publicstaticvoidmain(Stringargs[]){circlec1=newcircle();System.out.println("圆的的半径"+c1.getRadius()+c1.toString());Trianglet=newTriangle(3,4,5);System.out.println("三角形周长:"+t.getPerimeter());if(t.isTriangle())System.out.println("
5、三角形面积"+t.getArea());rectangler=newrectangle(4,5);System.out.print("矩形"+t.toString());}
此文档下载收益归作者所有