java泛型的一个错误求解

发布于 2021-11-13 10:06:19 字数 1486 浏览 714 评论 4

对于如下代码为什么eclipse会提示:K cannot be resolved to a type

public class GenericTest<T>
{
    private Generic3<K> foo1 ;
   

    public Generic3<K> getFoo1()
    {
        return foo1;
    }


    public void setFoo1(Generic3<K> foo1)
    {
        this.foo1 = foo1;
    }
   


    public static void main(String[] args)
    {
        Generic3<Integer>  generic = new Generic3<Integer>() ;
        generic.setFoo(new Integer(4)) ;
       
        GenericTest<String>  test = new GenericTest<String>();
       
    }
}
class Generic3<K>
{
    private K foo ;

    public K getFoo()
    {
        return foo;
    }

    public void setFoo(K foo)
    {
        this.foo = foo;
    }
   
   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终陌 2021-11-15 01:59:24

改为 public class GenericTest<K>

蓝颜夕 2021-11-15 01:26:25

V512 上面有这个教程,讲得很详细。

背叛残局 2021-11-14 22:47:07

应该给GenericTest类中的K指定具体类型。

建议不要在Java泛型上花费太多精力,其作用实在有限。

等风来 2021-11-14 20:09:20

应该是这样吧

public class GenericTest<T,K>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文