帮我找一下错误import java.util.*; public class Main { public static void main(String[] args) { Cylinder sc = new Cylinder(5.0, 6.0); sc.getArea(); sc.getVolume(); } } class Circle { double radius; public Circle() {} public Circle(double radius) { this.radius = radius; } public void getArea() { double area = Math.PI * radius * radius; return area; } } class Cylinder extends Circle { double height; public Cylinder(double radius, double height) { this.radius = radius; this.height = height; } public void getArea() { double area = Math.PI * radius * radius; return area; } public void getVolume() { double volume = getArea * height; return volume; } }
时间: 2023-06-04 08:02:36 浏览: 97
在第三个类Cylinder中,getArea方法和getVolume方法的返回值应该是double类型而不是void类型,同时在getVolume方法中需要调用getArea方法并获取其返回值而不是直接用getArea。正确代码应该是这样的:
import java.util.*;
public class Main {
public static void main(String[] args) {
Cylinder sc = new Cylinder(5.0, 6.0);
sc.getArea();
sc.getVolume();
}
}
class Circle {
double radius;
public Circle() {}
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
double area = Math.PI * radius * radius;
return area;
}
}
class Cylinder extends Circle {
double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double getArea() {
double area = 2 * Math.PI * radius * (radius + height);
return area;
}
public double getVolume() {
double volume = getArea() * height;
return volume;
}
}
阅读全文