繁体   English   中英

必须实现抽象方法

[英]Must implement abstract method

这是使用Processing 3.5,并不是每个java东西在这里都一样。
Bird类给我的错误是它需要实现call()。 它已经不是主要的吗? 我对界面没有经验,所以我不知道这里到底发生了什么。

 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

 }

我还将在此处结束这一点。 System.out.println(pol.call(pol.inputs, 1));
那行得通吗? 编译前不会出错。
感谢您的帮助。 请问是否没有什么意义,因为我仍然是新手,而不是java的最佳选择。 :)
主文件:

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

首先,您可以跳过FuncCall接口,使用Java的Supplier函数接口,只需将类对象getter的这些Suppliers方法引用分别添加到列表中。 另一种方法是提供一个具有x,y和size的吸气剂和/或成员变量的接口或抽象类,并将此接口或抽象类用作列表的类型参数。

  1. 与供应商一起使用:这更接近您的示例,并且需要更少的代码更改。 具有接口的第二个选项会完全更改您的Pol类,但我不确定这是否可以接受。

´

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

´然后是Pol类别´

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

´而且您的主体应该看起来像´

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

´

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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