[英]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的吸氣劑和/或成員變量的接口或抽象類,並將此接口或抽象類用作列表的類型參數。
´
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.