NWBL
幼苗
共回答了15个问题采纳率:86.7% 举报
public class Test {
public static void main(String[] args) {
MyPoint circle = new MyCircle(10, 20, 30);
System.out.println("area of circle :" + circle.getArea());
MyPoint sphere = new MySphere(10, 10, 10, 20);
System.out.println("area of sphere :" + sphere.getArea());
}
}
abstract class MyPoint {
public double x, y;
public MyPoint(double x, double y) {
this.x = x;
this.y = y;
}
abstract public double getArea();
}
class MyCircle extends MyPoint {
public double radius;
public MyCircle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
}
class MySphere extends MyPoint {
public double z, radius;
public MySphere(double x, double y, double z, double radius) {
super(x, y);
this.z = z;
this.radius = radius;
}
@Override
public double getArea() {
return 4 * Math.PI * radius * radius;
}
}
1年前
2