Class 'Test' is public, should be declared in a file named 'Test.java'
时间: 2024-01-14 15:05:10 浏览: 64
对不起,我之前的代码示例中的文件命名有误。确保将代码保存在名为 `Test.java` 的文件中,以匹配公共类的名称。以下是修正后的代码示例:
```java
class Square {
protected int width;
public Square(int width) {
this.width = width;
}
public int area() {
return width * width;
}
}
class Rectangle extends Square {
private int length;
public Rectangle(int width, int length) {
super(width);
this.length = length;
}
@Override
public int area() {
return width * length;
}
}
public class Test {
public static void main(String[] args) {
Square square = new Square(5);
Rectangle rectangle = new Rectangle(5, 8);
System.out.println("正方形面积:" + square.area());
System.out.println("长方形面积:" + rectangle.area());
}
}
```
请确保将以上代码保存在名为 `Test.java` 的文件中,然后编译和运行该文件。