JavaMyShape抽象类(面向对象)运行结果:(参考代码一)1.MyCircle类publicclassMyCircleextendsShape{privatefloatradius;publicMyCircle(intwidth,intheight){super(width,height);radius=width/2;this.x=3;this.y=45;//TODOAuto-generatedconstructorstub}@OverridepublicfloatgetArea(){//TODOAuto-generatedmethodstubreturn(float)(Math.PI*radius*radius);}@OverridepublicfloatgetGirth(){//TODOAuto-generatedmethodstubreturn(float)(Math.PI*2*radius);}publicStringtoString(){return这是圆,周长是:+getGirth()+面积是:+getGirth();}}2,MyEllipse类publicclassMyEllipseextendsShape{publicMyEllipse(intwidth,intheight){super(width,height);this.x=3;this.y=85;//TODOAuto-generatedconstructorstub}@OverridepublicfloatgetArea(){//TODOAuto-generatedmethodstubreturn(float)(Math.PI*(width+height)/4*(width+height)/4);}@OverridepublicfloatgetGirth(){//TODOAuto-generatedmethodstubreturn(float)((float)2*Math.PI*(width+height)/2);}publicStringtoString(){return这是一个椭圆,周长是:+getGirth()+面积是:+getGirth();}}3MyRectangle类publicclassMyRectangleextendsShape{publicMyRectangle(intwidth,intheight){super(width,height);this.x=3;this.y=5;//TODOAuto-generatedconstructorstub}@OverridepublicfloatgetArea(){//TODOAuto-generatedmethodstubreturnwidth*height;}@OverridepublicfloatgetGirth(){//TODOAuto-generatedmethodstubreturn2*width*height;}publicStringtoString(){return这是矩形,周长是:+getGirth()+面积是:+getGirth();}}4.MyTriangle类publicclassMyTriangleextendsShape{publicMyTriangle(intwidth,intheight){super(width,height);this.x=3;this.y=125;//TODOAuto-generatedconstructorstub}@OverridepublicfloatgetArea(){//TODOAuto-generatedmethodstubreturn(float)(width+height+Math.sqrt(width*width+height*height));}@OverridepublicfloatgetGirth(){//TODOAuto-generatedmethodstubreturnwidth*height*1/2;}publicStringtoString(){return这个是三角形,周长是:+getGirth()+面积是:+getGirth();}}5Shape抽象类publicabstractclassShape{publicintwidth;publicintheight;publicintx;publicinty;publicShape(intwidth,intheight){this.width=width;this.height=height;}publicabstractfloatgetArea();publicabstractfloatgetGirth();}6.Test类importjava.awt.Graphics;importjavax.swing.JFrame;importjavax.swing.JPanel;publicclassTestextendsJPanel{privateShapecircle;privateShapeellipse;privateShapetriangle;privateShaperectangle;publicTest(){JFramef=newJFrame(形状家族);circle=newMyCircle(10,10);ellipse=newMyEllipse(10,10);triangle=newMyTriangle(10,10);rectangle=newMyRectangle(10,10);f.setVisible(true);f.add(this);f.setSize(600,600);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}/***@paramargs*/publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubnewTest();}@OverrideprotectedvoidpaintComponent(Graphicsg){int[]xPoints=newint[]{triangle.x,triangle.x+20,triangle.x};int[]yPoints=newint[]{triangle.y,triangle.y,triangle.y-20};g.drawOval(circle.x,circle.y,circle.width,circle.height);g.drawString(circle.toString(),circle.x+25,circle.y+9);g.drawOval(ellipse.x,ellipse.y,ellipse.width,ellipse.height);g.drawString(ellipse.toString(),ellipse.x+25,ellipse.y+9);g.drawPolygon(xPoints,yPoints,3);g.drawString(triangle.toString(),triangle.x+25,triangle.y+9);g.drawRect(rectangle.x,rectangle.y,rectangle.width,rectangle.height);g.drawString(rectangle.toString(),rectangle.x+25,rectangle.y+9);}}效果:(参考代码二)1.源代码:1.1Myshapepackagecourse.java.shape;publicabstractclassMyShape{publicabstractfloatarea();publicabstractfloatperimeter();publicabstractvoiddisplay();}1.2MyRectanglepackagecourse.java.shape;publicclassMyRectangleextendsMyShape{privatefloatwidth,height;publicMyRectangle(floatwidth,floatheight){super();this.width=width;this.height=height;}publicfloatarea(){returnwidth*height;}publicfloatperimeter(){return2*(width+height);}publicvoiddisplay(){System.out.println(矩形);System.out.println(长:+width+,宽:+height);}}1.3MyCirclepackagecourse.java.shape;publicclassMyCircleextendsMyShape{privatefinalfloatPI=3.14f;privatefloatradius;publicMyCircle(floatradius){super();this.radius=radius;}publicfloatarea(){returnPI*radius*radius;}publicfloatperimeter(){return2*PI*radius;}publicvoiddisplay(){System.out.println(圆形);System.out.println(半径:+radius);}}1.4MyEllipsepackagecourse.java.shape;publicclassMyEllipseextendsMyShape{privatefinalfloatPI=3.14f;privatefloataLong,bShort;publicMyEllipse(floata,floatb){super();this.aLong=a;this.bShort=b;}publicfloatarea(){return1.0f/4*PI*aLong*bShort;}publicfloatperimeter(){returnPI*bShort+2*(aLong-bShort);}publicvoiddisplay(){System.out.println(椭圆形);System.out.println(长轴长:+aLong+,短轴长:+bShort);}}1.5MyTrianglepackagecourse.java.shape;publicclassMyTriangleextendsMyShape{privatefloata,b,c;publicMyTriangle(floata,floatb,floatc){super();this.a=a;this.b=b;this.c=c;}publicfloatarea(){floatp=(a+b+c)/2;return(float)Math.sqrt(p*(p-a)*(p-b)*(p-c));}publicfloatperimeter(){returna+b+c;}publicvoiddisplay(){System.out.println(三角形);System.out.println(边长:+a+,+b+,+c);}}1.6Test测试程序packagecourse.java.shape;importjava.awt.Color;importjava.awt.Graphics;importjavax.swing.JFrame;publicclassTestextendsJFrame{publicTest(){super(绘图演示);setSize(480,300);setVisible(true);}publicvoidpaint(Graphicsg){super.paint(g);g.setColor(Color.r