我有一个无法调用的界面方法

时间:2017-03-24 19:29:28

标签: java interface compiler-errors

我的班级Cupple需要调用实现接口beStored(char)的类DataIterator的方法StorableData

此接口的代码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);
    }
}

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

问题是编译器告诉我他&#34;无法解析方法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;

    }

}

2 个答案:

答案 0 :(得分:2)

  

问题是编译器告诉我他&#34;无法解决   方法beStored(int)。

这仅表示您尝试将int类型传递给beStored方法。如果再次查看此方法的界面定义,您将注意到您未遵守已设置的合同。

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

答案 1 :(得分:0)

问题是这样,在DataIterator中StorableData只是一个泛型类型,不是类StorableData,与DataIterator相同

以下代码将被编译。

public class DataIterator {

    private StorableData root_storable_data;

    public List<StorableData> iterate() {
            char read_character='x';
            this.root_storable_data.beStored(read_character);
            return null;
    }
}