Java 创建 class 的数组列表并调用 class 的方法

Java making an arraylist of class and calling a method of that class

到目前为止,我正在尝试创建一个 ArrayList 个对象,这些对象共享我想按 ArrayList.

顺序调用的相同方法

到目前为止代码是这样的

public class Shape extends Application {
   public void do(GraphicsContext canvas, int size, Color color){
     ;
   } 
}


public class Triangle extends Shape {
   @Override
   public void do(GraphicsContext canvas, int size, Color color){
      canvas.setFill(Color.WHITE);
      double[] xs = {60,80.0,50.0};
      double[] ys = {60,120.0,50.0};
      canvas.fillPolygon(xs,ys,3);
   }
}

而自动启动的主要 Class 是这样的

public class Main {
    public void drawForegroundContent(GraphicContext canvas){
       ArrayList<Shape> shpes = new ArrayList<Shape>();
       Triangle t = new Triangle();
       shapes.add(t);
       shapes.add(t);

       for (Shape k : shapes){
          k.do(canvas,CoreColor.BLACK, 80);
       }
    }
}

然而错误是<identifier> expected k.do(canvas, CoreColor.BLACK, 80)

此外,它对 Shape-class 的 void do 上的标识符有类似的抱怨。这段代码到底有什么问题?

根据 docs, do is a keyword (As in the context of a do-while loop). You need to name your method something else. As the JLS 关于关键字的说法:

50 character sequences, formed from ASCII letters, are reserved for use as keywords and cannot be used as identifiers


另请注意,调用该方法时参数顺序不正确。当方法接受 GraphicContextintColor 时,您正在传递 GraphicContextColorint