《JavaMyShape抽象类面向对象》由会员分享,可在线阅读,更多相关《JavaMyShape抽象类面向对象(11页珍藏版)》请在金锄头文库上搜索。
1、文档供参考,可复制、编制,期待您的好评与关注! Java MyShape抽象类(面向对象)运行结果:(参考代码一)1.MyCircle 类public class MyCircle extends Shapeprivate float radius;public MyCircle(int width, int height) super(width, height);radius=width/2;this.x=3;this.y=45;/ TODO Auto-generated constructor stubOverridepublic float getArea() / TODO Auto-
2、generated method stubreturn (float) (Math.PI*radius*radius);Overridepublic float getGirth() / TODO Auto-generated method stubreturn (float) (Math.PI*2*radius);public String toString()return 这是圆,周长是:+getGirth()+ 面积是:+getGirth();2,MyEllipse类public class MyEllipse extends Shapepublic MyEllipse(int widt
3、h, int height) super(width, height);this.x=3;this.y=85;/ TODO Auto-generated constructor stubOverridepublic float getArea() / TODO Auto-generated method stubreturn (float) (Math.PI *(width+height)/4*(width+height)/4);Overridepublic float getGirth() / TODO Auto-generated method stubreturn (float) (fl
4、oat)2*Math.PI*(width+height)/2);public String toString()return 这是一个椭圆,周长是:+getGirth()+ 面积是:+getGirth();3 MyRectangle类public class MyRectangle extends Shapepublic MyRectangle(int width, int height) super(width, height);this.x=3;this.y=5;/ TODO Auto-generated constructor stubOverridepublic float getAr
5、ea() / TODO Auto-generated method stubreturn width*height;Overridepublic float getGirth() / TODO Auto-generated method stubreturn 2*width*height;public String toString()return 这是矩形,周长是:+getGirth()+ 面积是:+getGirth();4.MyTriangle类public class MyTriangle extends Shapepublic MyTriangle(int width, int hei
6、ght) super(width, height);this.x=3;this.y=125;/ TODO Auto-generated constructor stubOverridepublic float getArea() / TODO Auto-generated method stubreturn (float) (width+height+Math.sqrt(width*width+height*height);Overridepublic float getGirth() / TODO Auto-generated method stubreturn width*height*1
7、/2;public String toString()return 这个是三角形,周长是:+getGirth()+ 面积是:+getGirth();5 Shape抽象类public abstract class Shape public int width;public int height;public int x;public int y;public Shape(int width, int height)this.width=width;this.height=height;public abstract float getArea();public abstract float ge
8、tGirth();6.Test类import java.awt.Graphics;import javax.swing.JFrame;import javax.swing.JPanel;public class Test extends JPanelprivate Shape circle;private Shape ellipse;private Shape triangle;private Shape rectangle;public Test()JFrame f=new JFrame(形状家族);circle=new MyCircle(10,10);ellipse=new MyEllip
9、se(10,10);triangle=new MyTriangle(10,10);rectangle=new MyRectangle(10,10);f.setVisible(true);f.add(this);f.setSize(600,600);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);/* * param args */public static void main(String args) / TODO Auto-generated method stubnew Test();Overrideprotected void paint
10、Component(Graphics g)int xPoints=new inttriangle.x,triangle.x+20,triangle.x;int yPoints=new inttriangle.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, ellip
11、se.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, rectangl
12、e.y+9);效果:(参考代码二)1. 源代码:1.1 Myshapepackage course.java.shape;public abstract class MyShape public abstract float area(); public abstract float perimeter(); public abstract void display();1.2 MyRectanglepackage course.java.shape;public class MyRectangle extends MyShapeprivate float width,height; publ
13、ic MyRectangle(float width, float height) super();this.width = width;this.height = height; public float area() return width*height; public float perimeter() return 2*(width+height); public void display() System.out.println(矩形); System.out.println(长: + width + , 宽: + height); 1.3 MyCirclepackage course.java.shape;public class MyCircle extends MyShapeprivate final float PI=3.14f; private float radius; public MyCircle(float radius) super();this.radius = radius; public float area() return PI*radius*radius; public float perimeter()