实验四 java继承与多态.docxVIP

  • 2
  • 0
  • 约2.16千字
  • 约 4页
  • 2024-07-13 发布于上海
  • 举报

//实验四第一题源程序classPoint2D

{

intx;inty;

Point2D(intx,inty)

{

this.x=x;this.y=y;

}

voidoffset(inta,intb)

{

x=x+a;y=y+b;

System.out.println(平移后:+x+,+y);

}

voidprint(Point2Dp2d1,Point2Dp2d2)

{

doubleL1=Math.sqrt(Math.pow((p2d1.x-p2d2.x),2)+Math.pow((p2d1.y-p2d2.y),2));System.out.println(L1=+L1);

}

}

classPoint3DextendsPoint2D

{

intz;

Point3D(intx,inty,intz)

{

super(x,y);this.z=z;

}

Point3D(Point2Dp,intz)

{

super(p.x,p.y);this.z=z;

}

voidoffset(inta,intb,intc)

{

x=x+a;y=y+b;z=z+c;

}

voidprint(Point3Dp3d1,Point3Dp3d2)

{

double

L2=Math.sqrt(Math.pow((p3d1.x-p3d2.x),2)+Math.pow((p3d1.y-p3d2.y),2)+Math.pow((p3d1.z-p3d2.z),2));

System.out.println(L2=+L2);

}

publicstaticvoidmain(String[]args)

{

Point2Dp2d1=newPoint2D(2,3);Point2Dp2d2=newPoint2D(4,5);Point3Dp3d1=newPoint3D(2,3,4);Point3Dp3d2=newPoint3D(4,5,6);p2d1.print(p2d1,p2d2);p3d1.print(p3d1,p3d2);

}

}

//实验四第二题源程序abstractclassShape

{

abstractpublicvoidshowArea();

}

interfaceDiagArea

{

doublegetDiagonal();doublegetArea();

}

classRectangleimplementsDiagArea

{

intw,h;

Rectangle(inti,intj)

{

w=i;h=j;

}

publicvoidshowArea()

{

ints1=w*h;

System.out.println(s矩形=+s1);

}

publicdoublegetDiagonal()

{

doubleL=Math.sqrt(w*w+h*h);System.out.println(对角线长=+L);returnL;

}

publicdoublegetArea()

{

doubles=w*h;

System.out.println(S矩形面积=+s);returns;

}

}

classSquareextendsRectangle

{intx;

Square(intx)

{

super(x,x);//此处修改前为x=i;this.x=x;

}

publicvoidshowArea()

{

ints2=x*x;

System.out.println(s正方形=+s2);

}

}

classCircle

{

intr;

Circle(inti)

{

r=i;

}

publicvoidshowArea()

{

doubles3=Math.PI*(r*r);System.out.println(s圆=+s3);

}

voidshow()

{

System.out.println(圆的半径=+r);

}

}

classAreas

{

publicstaticvoidmain(String[]args)

{

RectangleRe=newRectangle(2,3);SquareSq=newSquare(4);

CircleCi=newCircle(5);Re.showArea();

System.out.println(实现接口运算);

doubleL=Re.getDiagonal();doubleS=Re.getArea();System.out.println(\n);Sq.showArea();

System

文档评论(0)

1亿VIP精品文档

相关文档