public interface Shape { } class Circle implements Shape { Circle(int radius) { /* ... */ } } class Rectangle implements Shape { Rectangle(int height, int width) { /* ... */ } } public class Main { public static void main (String[] args) { Shape[] shapes = new Shape[2]; shapes[0] = new Circle(1); shapes[1] = new Rectangle(1, 2); writeShapes(shapes); } public static void writeShapes(Shape[] shapes){ for(Shape shape:shapes){ if(shape instanceof Circle) System.out.println("Circle"); else if(shape instanceof Rectangle) System.out.println("Rectangle"); } } 在示例中,我想添加新形状。但是,我无法理解在我的 main 方法中发生的错误“公共类型 Main 必须在其自己的文件中定义”是怎么回事。我试图在 main 前面添加“静态”,但它不起作用!任何建议表示赞赏!
添加回答
举报
0/150
提交
取消