参照教材例6.1,定义Point(点)类,由Point类派生出Circle(圆)类,再由Circle类派生出Cylinder(圆柱体

2024-10-29 05:38:38
推荐回答(1个)
回答1:

public class Point2 {//瞎弄的几个 我们的书不一样。
int x,y;
public Point2(int a,int b){
x = a;
y = b;
}
public String toString(){
return "("+x+","+y+")";
}

public static void main(String args[]){
Point2 p[] = new Point2[3];
p[0] = new Point2(0,0);
p[1] = new Circle3(0,0,4);
p[2] = new Cylinder(0,0,4,5);
for(int i = 0; i<3; i++){
System.out.println(p[i].toString());
}

}

}

class Circle3 extends Point2{
int r;
public Circle3(int x,int y,int a){
super(x,y);
r = a;
}
public float getArea(){
return (float)(Math.PI*r*r);
}
}
class Cylinder extends Point2{
int h,r;
public Cylinder(int x,int y,int a,int b){
super(x,y);
h = a;
r = b;
}
public float getV(){
return (float)(Math.PI*r*r*h);
}

}