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

Hjell :

So far I'm trying to make an ArrayList of objects that share the same method which I want to call in order of the ArrayList.

So far the code is like this

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);
   }
}

And the main Class which launches automatically goes like this

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);
       }
    }
}

However the error is <identifier> expected k.do(canvas, CoreColor.BLACK, 80)

Furthermore the it complains similarly on identifier on void do of Shape-class. What exactly is wrong in this code?

GBlodgett :

According to the 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 say in regard to keywords:

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


Also side note you have the arguments out of order when you call the method. You are passing GraphicContext, Color, int, when the method accepts GraphicContext, int, Color.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111493&siteId=1