对象到 ArrayList

Objects to ArrayList

我想做的就是将对象添加到列表中。但我不知道为什么 shapetizer.add(circleone); 行会出错。谁能帮帮我。

主要

import java.util.*;

public class Main {

    //create an empty list of shape objects
    ArrayList<Shape> shapetizer = new ArrayList<Shape>();

    //create 2 circle objects
    Circle circleone = new Circle(10);
    Circle circletwo = new Circle(20);

    //Add the created circles to the list
    shapetizer.add(circleone);

}

形状

public abstract class Shape {

    // yet to be filled

}

圆形

public class Circle extends Shape {

    private float radius;

    public Circle(float radius) {
        super();
        this.radius = radius;
    }

    public String toString() {
        return "Circle [radius=" + radius + "]";
    }

}

我在上面提到的行中遇到的错误是:

您的代码在 Main 中 class 不在 main 方法中!