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 方法中發(fā)生的錯誤“公共類型 Main 必須在其自己的文件中定義”是怎么回事。我試圖在 main 前面添加“靜態(tài)”,但它不起作用!任何建議表示贊賞!
添加回答
舉報
0/150
提交
取消