java – 从通用通配符引用调用方法
发布时间:2020-12-15 04:27:05 所属栏目:Java 来源:网络整理
导读:以下代码简化为问题的基本要点: public class GenericTest { private interface CopyInterfaceT extends CopyInterfaceT { public void copyFrom(T source); } public void testMethod() { CopyInterface? target; CopyInterface? source; target.copyFrom(
以下代码简化为问题的基本要点:
public class GenericTest { private interface CopyInterface<T extends CopyInterface<T>> { public void copyFrom(T source); } public void testMethod() { CopyInterface<?> target; CopyInterface<?> source; target.copyFrom(source); } } 这会导致以下编译器错误消息: GenericTest.CopyInterface类型中的copyFrom(capture#1-of?)方法不适用于参数(GenericTest.CopyInterface)GenericTest.java / ACAF / src / de / tmasoft / acaftest / attributes第14行Java问题 当我使用原始类型作为变量目标时,我只得到一个原始类型的警告.有没有办法在不使用原始类型的情况下编译? 解决方法
你必须使你的testMethod()通用,并声明源和目标是相同的类型:
public <T extends CopyInterface<T>> void testMethod() { T target = ...; //initialize the variable T source = ...; //initialize the variable target.copyFrom(source); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |