This question already has answers here:
My program keeps saying that the method cannot be resolved

(2个答案)


3年前关闭。




我的类Cupple需要调用实现接口(interface)beStored(char)的类DataIterator的方法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)方法。

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

完整代码。

接口(interface):
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;
    }
}

类:
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;

    }

}

最佳答案



这只是意味着您正在尝试将int类型传递给beStored方法。如果再次查看此方法的接口(interface)定义,您会发现您没有遵守已设置的契约(Contract)。

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

10-01 08:50
查看更多