调用泛型java方法时出现编译器错误
我创建了一个泛型类,其中包含很少的方法。我只想调用该方法并添加值,但它不起作用。这是密码调用泛型java方法时出现编译器错误,java,generics,Java,Generics,我创建了一个泛型类,其中包含很少的方法。我只想调用该方法并添加值,但它不起作用。这是密码 public interface GenericInterface<T> { public T getValue(); public void setValue(T t); } public class FirstGeneric<T> implements GenericInterface<T> { private T value;
public interface GenericInterface<T> {
public T getValue();
public void setValue(T t);
}
public class FirstGeneric<T> implements GenericInterface<T> {
private T value;
public FirstGeneric(T _value) {
this.value = _value;
}
@Override
public T getValue() {
return value;
}
@Override
public void setValue(T t) {
this.value = t;
}
@SuppressWarnings("unchecked")
public static <T> T addValues(FirstGeneric<T> myGeneric, FirstGeneric<T> myGeneric1) {
T result = null;
if (myGeneric != null && myGeneric1 != null) {
T t1 = myGeneric.getValue();
T t2 = myGeneric1.getValue();
result = (T) t1.toString().concat(t2.toString());
System.out.println("Result is:=" + result);
}
return result;
}
public static void main(String args[]) {
Integer int1 = 1;
FirstGeneric<Integer> myInt = new FirstGeneric<Integer>(int1);
String value = "Hello";
FirstGeneric<String> myString = new FirstGeneric<String>(value);
addValues(myString, myInt);
}
}
公共接口通用接口{
公共T getValue();
公共无效设置值(T);
}
公共类FirstGeneric实现泛型接口{
私人T值;
公共第一通用(T_值){
this.value=_值;
}
@凌驾
公共T getValue(){
返回值;
}
@凌驾
公共无效设置值(T){
该值=t;
}
@抑制警告(“未选中”)
公共静态T addValues(FirstGeneric myGeneric,FirstGeneric myGeneric1){
T结果=null;
if(myGeneric!=null&&myGeneric1!=null){
T t1=myGeneric.getValue();
t2=myGeneric1.getValue();
结果=(T)t1.toString().concat(t2.toString());
System.out.println(“结果为:=”+结果);
}
返回结果;
}
公共静态void main(字符串参数[]){
整数int1=1;
FirstGeneric myInt=新的FirstGeneric(int1);
String value=“Hello”;
FirstGeneric myString=新的FirstGeneric(值);
addValues(myString,myInt);
}
}
但我在最后一行遇到编译错误
FirstGeneric类型中的方法addValues(FirstGeneric,FirstGeneric)不适用于参数(FirstGeneric,FirstGeneric)
我做错了什么?
谢谢。
addValues
只有一个泛型类型参数,这意味着您不能同时向它传递FirstGeneric
和FirstGeneric
要使addValues(myString,myInt)
调用有效,可以在方法中定义两个泛型类型参数:
public static <T,U> String addValues(FirstGeneric<T> myGeneric, FirstGeneric<U>
myGeneric1) {
String result = null;
if (myGeneric != null && myGeneric1 != null) {
T t1 = myGeneric.getValue();
U t2 = myGeneric1.getValue();
result = t1.toString().concat(t2.toString());
System.out.println("Result is:=" + result);
}
return result;
}
publicstaticstringaddvalues(firstgenericmygeneric,FirstGeneric
myGeneric1){
字符串结果=null;
if(myGeneric!=null&&myGeneric1!=null){
T t1=myGeneric.getValue();
U t2=myGeneric1.getValue();
结果=t1.toString().concat(t2.toString());
System.out.println(“结果为:=”+结果);
}
返回结果;
}
请注意,我将返回类型更改为
String
,因为它显然是String
(由t1.toString().concat(t2.toString())
生成),所以您不能将其强制转换为t
,希望t
将是StringaddValues
具有一个通用类型参数,这意味着您不能同时向它传递FirstGeneric
和FirstGeneric
要使addValues(myString,myInt)
调用有效,可以在方法中定义两个泛型类型参数:
public static <T,U> String addValues(FirstGeneric<T> myGeneric, FirstGeneric<U>
myGeneric1) {
String result = null;
if (myGeneric != null && myGeneric1 != null) {
T t1 = myGeneric.getValue();
U t2 = myGeneric1.getValue();
result = t1.toString().concat(t2.toString());
System.out.println("Result is:=" + result);
}
return result;
}
publicstaticstringaddvalues(firstgenericmygeneric,FirstGeneric
myGeneric1){
字符串结果=null;
if(myGeneric!=null&&myGeneric1!=null){
T t1=myGeneric.getValue();
U t2=myGeneric1.getValue();
结果=t1.toString().concat(t2.toString());
System.out.println(“结果为:=”+结果);
}
返回结果;
}
注意,我将返回类型更改为String
,因为它显然是String
(由t1.toString().concat(t2.toString())
生成),所以不能将其强制转换为t
,希望t
将是字符串