使用泛型返回类型时出错(不兼容的类型:无法转换为t)

omhiaaxx  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(505)
public class Main{
    private MyQueue queue = null;
    private MyStack stack = null;
    private MyList list = null;

    public static void main(String args[]){
        Main m = new Main();
        m.run();
    }

    public void run(){
        // only one data structure is not null.
        // for example, let's suppose that it's queue
        this.queue = new MyQueue();

        int intToAdd = 6;   // I want to add this number
        this.selectStruct().insert(intToAdd);       // I want to invoke the "insert" method of the current data structure in use
    }

    private <T> T selectStruct(){       // this should check with data structure is in use (so which of them is not null) and then return the reference
        if (this.queue!=null){
            return this.queue;
        }
        if (this.stack!=null){
            return this.stack;
        }
        if (this.list!=null){
            return this.list;
        }
    }
}

我想用这个叫做 selectStruct 选择当前使用的数据结构并返回引用。从这个参考号打电话给 insert 调用正确类的正确insert方法。
问题是我有几个错误。我从未使用过泛型,所以我有点困惑。

$ javac Main.java
Main.java:22: error: incompatible types: MyQueue cannot be converted to T
                        return this.queue;
                                   ^
  where T is a type-variable:
    T extends Object declared in method <T>selectStruct()
Main.java:25: error: incompatible types: MyStack cannot be converted to T
                        return this.stack;
                                   ^
  where T is a type-variable:
    T extends Object declared in method <T>selectStruct()
Main.java:28: error: incompatible types: MyList cannot be converted to T
                        return this.list;
                                   ^
  where T is a type-variable:
    T extends Object declared in method <T>selectStruct()
Main.java:17: error: cannot find symbol
                this.selectStruct().insert(intToAdd);
                                   ^
  symbol:   method insert(int)
  location: class Object
4 errors
lnlaulya

lnlaulya1#

MyQueue , MyStack ,和 MyList 实现包含所有数据结构要实现的方法的相同接口,例如:

public interface IDataStructure {
    void insert(int intToAdd);
}

你的课程应该是这样的:

public class MyQueue implements IDataStructure{

    @Override
    public void insert(int intToAdd) {
        // TODO 
    }

}

public class MyStack implements IDataStructure{

    @Override
    public void insert(int intToAdd) {
        // TODO 
    }

}

public class MyList implements IDataStructure{

    @Override
    public void insert(int intToAdd) {
        // TODO 
    }

}

最后,您可以修改 selectStruct() 返回idatastructure的方法:

private IDataStructure selectStruct() {
    if (this.queue != null) {
        return this.queue;
    }
    if (this.stack != null) {
        return this.stack;
    }
    if (this.list != null) {
        return this.list;
    }
    return null;
}
4smxwvx5

4smxwvx52#

当您用类型参数t(如 private <T> T selectStruct() ),您的意思是此方法的用户可以选择要使用此方法的任何类型。
所以在我们的身体里 selectStruct ,您不知道调用者将使用哪种类型,当然也不能返回任何特定类型。所以你的每一个退货条款 return this.queue; 将产生错误。

相关问题