简体   繁体   中英

Must implement abstract method

This is using Processing 3.5, not every java thing works the same here.
The Bird class is giving me the error saying it needs to implement call(). Isn't it already under the main? I'm not experienced with interfaces so I don't know what exactly is going on here.

 public interface FuncCall<A> {
   A call();
 }

 class Bird implements FuncCall{
    //Error here ^
    //The type FuncCallTest.Bird must implement the inherited abstract method FuncCallTest.FuncCall.call()
    //Is this not implemented already under main?

   float x, y, size;
   ArrayList<FuncCall<Float>> inputs = new ArrayList<FuncCall<Float>>();

   public Bird(float x, float y, float size){
     this.x = x;
     this.y = y;
     this.size = size;
   }

   public void main(String[] args){

     FuncCall<Float> getX = new FuncCall<Float>(){
       @Override
       public Float call(){
           return x;
         }
     };

     FuncCall<Float> getY = new FuncCall<Float>(){
       @Override
       public Float call(){
         return y;
       }
     };

     FuncCall<Float> getSize = new FuncCall<Float>(){
       @Override
       public Float call(){
         return size;
       }
     };

     inputs.add(getX);
     inputs.add(getY);
     inputs.add(getSize);

   }

 }

 class Pol {

   ArrayList<FuncCall<Float>> inputs = new ArrayList<FuncCall<Float>>();

   public Pol(ArrayList<FuncCall<Float>> inputs){
     this.inputs = inputs;
   }

   //public float call(ArrayList<FuncCall<Float>> arr, int index){
     //return arr.get(index).call();
   //}
   //How do I do this? Do I need to implement the interface here as well? Because if so same error as on Bird

 }

I'll also stick this extra bit on the end here. System.out.println(pol.call(pol.inputs, 1));
Does will that work? It doesn't error before compiling.
I appreciate any help. Please ask if something doesn't make sense as I'm still new to stack and not the best with java. :)
main file :

 void setup(){

   Bird bird = new Bird(1.2, 3.2, 7.5);
   Pol pol = new Pol(bird.inputs);
   System.out.println(pol.call(pol.inputs, 1););
 }

First of all you could skip your FuncCall interface and use Java's Supplier functional interface and just add these Suppliers respectively method references of your class objects getters to the list. Another approach is to provide an interface or abstract class that has getters and/or member variables for x, y and size and use this interface or abstract class as type parameter for the list.

  1. With Suppliers: This is closer to your example and requires less changes in your code. The second option with an interface changes your Pol class completely and I am not sure if this is acceptable for you.

´

public class Bird {

      private float x;
      private float y;
      private float size;

       public Bird(float x, float y, float size) {
           //set your members here
       }

       public Float getX() {
            return this.x;
        }

        public Float getY() {
            return this.y;
        }

         public Float getSize() {
             return this.size;
          }
}

´ Then the Pol class ´

public class Pol {

    private final List<Supplier<Float>> inputs;

    public Pol(List<Supplier<Float>> inputs) {
         this.inputs = inputs;
     }

     public Float call(int index) {
         return this.inputs.get(index).get();
     }
}

´ And your main should look like ´

public static int main(String[] args) {

    Bird bird = new Bird(1.0f, 1.0f, 2.5f);

     Pol pol = new Pol(Arrays.asList(bird::getX, 
     bird::getY, bird::getSize));
     Float birdsSize = pol.call(2);

     return 0;
}

´

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM