使用泛型 return 类型时出错(不兼容的类型:无法转换为 T)
Error using generic return type (incompatible types: cannot be converted to T)
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
的方法来 select 当前使用的数据结构和 return 引用。然后从这个引用调用 insert
调用正确 class.
的正确插入方法
问题是我有几个错误。我从来没有用过泛型,所以我有点困惑。
$ 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
让 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()
方法修改为 return 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;
}
当你声明一个带有类型参数 T 的方法时(比如 private <T> T selectStruct()
),你是说这个方法的用户可以选择他们想要使用这个方法的任何类型。
所以在selectStruct
的正文中,你不知道调用者会使用哪种类型,当然你也不能return任何特定的类型。这就是为什么像 return this.queue;
这样的每个 return 子句都会产生错误。
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
的方法来 select 当前使用的数据结构和 return 引用。然后从这个引用调用 insert
调用正确 class.
问题是我有几个错误。我从来没有用过泛型,所以我有点困惑。
$ 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
让 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()
方法修改为 return 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;
}
当你声明一个带有类型参数 T 的方法时(比如 private <T> T selectStruct()
),你是说这个方法的用户可以选择他们想要使用这个方法的任何类型。
所以在selectStruct
的正文中,你不知道调用者会使用哪种类型,当然你也不能return任何特定的类型。这就是为什么像 return this.queue;
这样的每个 return 子句都会产生错误。