Java中的难题,哪位高手帮忙解决一下

2025-03-03 10:49:02
推荐回答(2个)
回答1:

这是我以前写过的程序,和你那个挺像,名字什么的你自己改改就好。

class Circle extends Shape {
final double PI_VALUE = 3.14;
double radius;
Circle(double radius) {
this.radius = radius;
}
Circle() {

}
double area() {
return PI_VALUE * this.radius * this.radius;
}
}
class Square extends Shape {
double length;
Square(double length) {
this.length = length;
}
Square() {

}
double area() {
return this.length * this.length;
}
}
public class ShowArea {
public static void main(String[] args) {
Shape objCircle = new Circle(2);
System.out.println("圆的面积为" + objCircle.area());
Shape objSquare = new Square(2);
System.out.println("正方形的面积为" + objSquare.area());
}
}

回答2:

一楼的就不错。