gpt4 book ai didi

java - 我有一个无法调用的接口(interface)方法

转载 作者:行者123 更新时间:2023-12-02 11:15:52 24 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





My program keeps saying that the method cannot be resolved

(2 个回答)


5年前关闭。




我的类(class)Cupple需要调用方法beStored(char)一类DataIterator实现接口(interface)StorableData .

这里是这个接口(interface)的代码StorableData :

package general_classes.cupples;    
public interface StorableData {

public void beStored(char c);

}

这里是实现的代码:
package general_classes.cupples;
public class Cupple<TYPE_OF_ELEMENTS> implements StorableData {
public void beStored(char c) {

}
}

最后,这里是类的代码 DataIterator :
package general_classes.DataIteration;
public class DataIterator<StorableData> {
private StorableData root_storable_data;
public List<StorableData> iterate() {
this.root_storable_data.beStored(read_character);
}
}

请注意,我没有写完所有的行。

问题是编译器告诉我他“无法解析方法 beStored(int)

但是,如您所见,它实际上在界面中。所以有什么问题 ?

完整的代码。

界面 :
package general_classes.cupples;

public interface StorableData {

public Cupple beStored(int c);

}

执行 :
package general_classes.cupples;

import java.util.ArrayList;

public class Cupple<TYPE_OF_ELEMENTS> extends ArrayList<TYPE_OF_ELEMENTS> implements StorableData {
private int position_to_insert_element;
private int number_of_elements;
private Cupple<TYPE_OF_ELEMENTS> next_cupple;
private Cupple<TYPE_OF_ELEMENTS> current_empty_cupple;

public Cupple(int number_of_elements) {
this.position_to_insert_element = 0;
this.number_of_elements = number_of_elements;
}

public Cupple beStored(int c) {
Cupple returned_cupple = this;

if(this.position_to_insert_element > this.number_of_elements) {
this.next_cupple = returned_cupple = new Cupple<>(this.number_of_elements);

} else {

//this.add((TYPE_OF_ELEMENTS) c);
this.position_to_insert_element++;

}

return returned_cupple;

}

public Cupple next() {
return this.next_cupple;
}
}

类(class) :
package general_classes.DataIteration;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
* Reads character per character some given data. Stores the character in a
* list, after having casted it in the specified type by the way.
*
* @author e1300478
*
* @param <StorableData>
* the wished type of the reading's returned elements
*/
public class DataIterator<StorableData> {

private Reader reader;
private List<StorableData> returned_elements_list;
private StorableData root_storable_data;

DataIterator(Reader reader, StorableData storable_data) {
this.reader = reader;
this.returned_elements_list = new ArrayList<>();
this.root_storable_data = storable_data;
}

public List<StorableData> iterate() throws IOException {

int read_character;
do {

read_character = this.reader.read();
StorableData storable_data = this.root_storable_data.beStored((int) read_character);
if(!this.returned_elements_list.contains(storable_data)) {
this.returned_elements_list.add(storable_data);
}

} while (read_character > -1);

return this.returned_elements_list;

}

}

最佳答案

The problem is that the compiler tells me that he "cannot resolve the method beStored(int).



这仅仅意味着您正在尝试传递 int输入 beStored方法。如果您再次查看此方法的接口(interface)定义,您会注意到您没有遵守已设置的约定。
public void beStored(char c);

在下面的代码 read_character很可能是 int类型而不是字符因此错误。
this.root_storable_data.beStored(read_character);

解决方案

改变这个:
int read_character;

对此:
char read_character;

也改变这个:
StorableData storable_data = this.root_storable_data.beStored((int) read_character);

对此:
StorableData storable_data = this.root_storable_data.beStored(read_character);

关于java - 我有一个无法调用的接口(interface)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43007687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com