警告:使用内部类时,类型参数E隐藏类型E. [英] Warning: The type parameter E is hiding the type E when using an inner class

查看:364
本文介绍了警告:使用内部类时,类型参数E隐藏类型E.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class Stack< E>这是一个包含静态节点和其他非静态类型的堆栈。实现了Iterable< E> 
{
private int N;
私人节点< E>第一;

private static class Node< E> //这工作正常。
{
私人E项;
私人节点< E>下一个;
}
}

但是当我试图让Node非静态,它给了我这个警告类型参数E隐藏了类型E。

  public class Stack< E>实现了Iterable< E> 
{
private int N;
私人节点< E>第一;

私人类节点< E> //这显示警告。
{
私人E项;
私人节点< E>下一个;




$ b $ p
$ b

我有一点理解,静态memeber是类的成员,所以它不会给我一个警告,但是当我使它非静态时,它属于实例。但它不是一个明确的想法。

解决方案

它警告您您正在使用通用参数名称 E 在已经定义的范围内。更改 Node 的通用参数名称将是解决警告的一种方法:

  public class Stack< E>实现了Iterable< E> 
{
private int N;
私人节点< E>第一;

私人类节点< T>
{
私人T项;
私人节点< T>下一个;






$ b

但是由于E已经存在,你应该只使用那;由于在泛型类型( Stack< object> .Node 和<$ c $)中定义了 Node c> Stack< String> .Node 是不同的类型):

  public class Stack< E>实现了Iterable< E> 
{
private int N;首先是
私有节点;

私人类节点
{
私人E项;
下一个私人节点;




$ b $ p
$ b

请注意,这是可行的,因为 Node 不是静态的,因此它有一个对外部 Stack< E> 对象的引用,并且由于这个 E必须定义。如果 Node 是静态的,那么它与外部 Stack< E> 类型没有实际关系,词法范围。

I was writing a stack, one with a static Node and other non-static.

public class Stack<E> implements Iterable<E>
{
    private int N;
    private Node<E> first;

    private static class Node<E>    // This works fine.
    {
        private E item;
        private Node<E> next;
    }
}

But when I try to make the Node non-static, it gives me this warning "The type parameter E is hiding the type E"

public class Stack<E> implements Iterable<E>
{
    private int N;
    private Node<E> first;

    private class Node<E>    // This shows warning.
    {
        private E item;
        private Node<E> next;
    }
}

A bit of understanding which I have tell me that since static memeber is a member of class so it does not give me a warning but when I make it non-static it belongs to the instance. But its not a clear thought.

解决方案

It's warning you that you are using the generic argument name E in a scope when it already is defined. Changing the generic argument name for Node would be one way to resolve the warning:

public class Stack<E> implements Iterable<E>
{
    private int N;
    private Node<E> first;

    private class Node<T>
    {
        private T item;
        private Node<T> next;
    }
}

But since E is already exists, you should just use that; Node is already generic due to being defined within a generic type (Stack<object>.Node and Stack<String>.Node are different types):

public class Stack<E> implements Iterable<E>
{
    private int N;
    private Node first;

    private class Node
    {
        private E item;
        private Node next;
    }
}

Note that this works because Node is not static, therefore it has a reference to the outer Stack<E> object, and because of this E must be defined. If Node is static then it has no real relationship to the outer Stack<E> type other than effectively being within its lexical scope.

这篇关于警告:使用内部类时,类型参数E隐藏类型E.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆