问题描述
急求源码啊!谢谢1.定义一个形状类,Point,Shape2.派生出矩形(Rectangle)、圆(Circle)、椭圆(Ellipse)3.完成其中可能存在的多态方法4.需要有每种形状的输出,包括中心点、面积、周长的输出这是我的作业哦。我不理解什么叫多态啊!
解决方案
解决方案二:
建议你找java图形编程的书来看。
解决方案三:
引用1楼ylz2007的回复:
建议你找java图形编程的书来看。
有这必要吗?我看楼主的意思是应用面向对象的啊!
解决方案四:
多态就是父类引用指向子类对象,运行是根据子类对象的实际类型来调用相应的方法。下面就是我简单些的一些代码,楼主可以参考参考。如有不足的地方,希望大家多多指教。publicclassPoint{privatedoublex;privatedoubley;publicPoint(doublex,doubley){this.x=x;this.y=y;}publicPoint(){this(0,0);}publicdoublegetX(){returnx;}publicvoidsetX(doublex){this.x=x;}publicdoublegetY(){returny;}publicvoidsetY(doubley){this.y=y;}@OverridepublicStringtoString(){return"坐标是:("+x+","+y+")";}}publicabstractclassShape{publicabstractvoidprintCenterPoint();//打印中心点publicabstractvoidprintArea();//打印面积publicabstractvoidprintPerimeter();//打印周长//该方法统一打印出Shape的相关信息publicvoidprint(){printCenterPoint();printArea();printPerimeter();}}publicclassRectangleextendsShape{privatePointpoint;//矩形左上角的点,用来定位privatedoublewidth;//矩形的宽度privatedoubleheight;//矩形的高度publicRectangle(Pointpoint,doublewidth,doubleheight){this.point=point;this.width=width;this.height=height;}publicRectangle(){this(newPoint(0,0),800,600);//默认左上角的位置为(0,0),宽度为800,高度为600}publicPointgetPoint(){returnpoint;}publicvoidsetPoint(Pointpoint){this.point=point;}publicdoublegetWidth(){returnwidth;}publicvoidsetWidth(doublewidth){this.width=width;}publicdoublegetHeight(){returnheight;}publicvoidsetHeight(doubleheight){this.height=height;}publicvoidprintArea(){System.out.println("矩形的面积是:"+width*height);}publicvoidprintCenterPoint(){doublex=point.getX()+width/2;doubley=point.getY()+height/2;System.out.println("矩形的中心点"+newPoint(x,y));}publicvoidprintPerimeter(){System.out.println("矩形的周长是:"+2*(width+height));}publicvoidprint(){System.out.println("矩形的相关信息");super.print();System.out.println();}}publicclassCircleextendsShape{privatestaticdoublepi=3.14;privatePointpoint;//圆的中心privatedoubleradius;publicCircle(Pointpoint,doubleradius){this.point=point;this.radius=radius;}publicCircle(){this(newPoint(),15);//默认圆的中心坐标为(0,0),半径为15}publicPointgetPoint(){returnpoint;}publicvoidsetPoint(Pointpoint){this.point=point;}publicdoublegetRadius(){returnradius;}publicvoidsetRadius(doubleradius){this.radius=radius;}publicvoidprintArea(){doublearea=pi*radius*radius;System.out.println("圆的面积为:"+area);}publicvoidprintCenterPoint(){System.out.println("圆的中心点"+point);}publicvoidprintPerimeter(){doubleperimeter=2*pi*radius;System.out.println("圆的周长是:"+perimeter);}publicvoidprint(){System.out.println("圆的相关信息");super.print();System.out.println();}}publicclassEllipseextendsShape{privatestaticdoublepi=3.14;privatePointpoint;//左上角的点,相当于和该椭圆外切的矩形的左上角的点。privatedoublea;//长半轴的长privatedoubleb;//短半轴的长publicEllipse(Pointpoint,doublea,doubleb){this.point=point;this.a=a;this.b=b;}publicEllipse(){this(newPoint(),8,6);//默认的左上角位置为(0,0),长半轴长为8,短半轴长为6}publicPointgetPoint(){returnpoint;}publicvoidsetPoint(Pointpoint){this.point=point;}publicdoublegetA(){returna;}publicvoidsetA(doublea){this.a=a;}publicdoublegetB(){returnb;}publicvoidsetB(doubleb){this.b=b;}publicvoidprintArea(){//S=π(圆周率)×a×b(其中a,b分别是椭圆的长半轴,短半轴的长).doublearea=pi*a*b;System.out.println("椭圆的面积为:"+area);}publicvoidprintCenterPoint(){doublex=point.getX()+a;doubley=point.getY()+b;System.out.println("椭圆的中心"+newPoint(x,y));}publicvoidprintPerimeter(){//椭圆的周长公式很多,这里采用比较简单的一个:L1=πQN/arctgN(b→a、Q=a+b、N=((a-b)/a)^2、)doubleq=a+b;doublen=Math.pow(((a-b)/a),2);doubleperimeter=pi*q*n/Math.atan(n);System.out.println("椭圆的周长为:"+perimeter);}publicvoidprint(){System.out.println("椭圆的相关信息");super.print();System.out.println();}}publicclassTest{publicstaticvoidmain(String[]args){Shapeshape=newRectangle();shape.print();shape=newCircle();shape.print();shape=newEllipse();shape.print();}}