Java MyShape抽象类 面向对象.docx
《Java MyShape抽象类 面向对象.docx》由会员分享,可在线阅读,更多相关《Java MyShape抽象类 面向对象.docx(13页珍藏版)》请在冰豆网上搜索。
JavaMyShape抽象类面向对象
JavaMyShape抽象类(面向对象)
运行结果:
(参考代码一)
1.MyCircle类
publicclassMyCircleextendsShape{
privatefloatradius;
publicMyCircle(intwidth,intheight){
super(width,height);
radius=width/2;
this.x=3;
this.y=45;
//TODOAuto-generatedconstructorstub
}
@Override
publicfloatgetArea(){
//TODOAuto-generatedmethodstub
return(float)(Math.PI*radius*radius);
}
@Override
publicfloatgetGirth(){
//TODOAuto-generatedmethodstub
return(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
}
@Override
publicfloatgetArea(){
//TODOAuto-generatedmethodstub
return(float)(Math.PI*(width+height)/4*(width+height)/4);
}
@Override
publicfloatgetGirth(){
//TODOAuto-generatedmethodstub
return(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
}
@Override
publicfloatgetArea(){
//TODOAuto-generatedmethodstub
returnwidth*height;
}
@Override
publicfloatgetGirth(){
//TODOAuto-generatedmethodstub
return2*width*height;
}
publicStringtoString(){
return"这是矩形,周长是:
"+getGirth()+"面积是:
"+getGirth();
}
}
4.MyTriangle类
publicclassMyTriangleextendsShape{
publicMyTriangle(intwidth,intheight){
super(width,height);
this.x=3;
this.y=125;
//TODOAuto-generatedconstructorstub
}
@Override
publicfloatgetArea(){
//TODOAuto-generatedmethodstub
return(float)(width+height+Math.sqrt(width*width+height*height));
}
@Override
publicfloatgetGirth(){
//TODOAuto-generatedmethodstub
returnwidth*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-generatedmethodstub
newTest();
}
@Override
protectedvoidpaintComponent(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.1Myshape
packagecourse.java.shape;
publicabstractclassMyShape{
publicabstractfloatarea();
publicabstractfloatperimeter();
publicabstractvoiddisplay();
}
1.2MyRectangle
packagecourse.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.3MyCircle
packagecourse.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.4MyEllipse
packagecourse.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.5MyTriangle
packagecourse.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.red);
g.drawRect(50,50,60,40);
g.setColor(Color.blue);
g.drawOval(150,50,70,40);
g.setColor(Color.black);
g.drawOval(250,100,60,60);
g.setColor(Color.green);
intxValues[]={100,150,200};
intyValues[]={200,180,240};
g.drawPolygon(xValues,yValues,3);
}
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
MyShapemc,mr,mt,me;
mc=newMyCircle(3);
mc.display();
System.out.println("圆形的面积为:
"+mc.area());
System.out.println("圆形的周长为:
"+mc.perimeter());
mr=newMyRectangle(4,8);
mr.display();
System.out.println("矩形的面积为:
"+mr.area());
System.out.println("矩形的周长为:
"+mr.perimeter());
mt=newMyTriangle(3,4,5);
mt.display();
System.out.println("三角形的面积为:
"+mt.area());
System.out.println("三角形的周长为:
"+mt.perimeter());
me=newMyEllipse(2,4);
me.display();
System.out.println("椭圆形的面积为:
"+me.area());
System.out.println("椭圆形的周长为:
"+me.perimeter());
JFrame.setDefaultLookAndFeelDecorated(true);
Testapplication=newTest();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
UML图: