简体   繁体   English

为什么内部类会抱怨泛型类型?

[英]Why does the inner class complain about generic type?

I have a class that implements Iterable so that the user can use an iterator. 我有一个实现Iterable的类,以便用户可以使用迭代器。 I'm using generics to allow the user to use any type and work with the class. 我正在使用泛型来允许用户使用任何类型并使用该类。

This is the working code below without warnings- 这是下面的工作代码,没有任何警告 -

public class CustomStackUsingArray<Type> implements CustomList<Type>, Iterable<Type> {

    Type []arr;

    public Iterator<Type> iterator() {
        return (new ListIterator());
    }

    private class ListIterator implements Iterator<Type>{
        //Implement Iterator here
    }

    //Implement other methods here
}

However, if I have the ListIterator defined like this- 但是,如果我将ListIterator定义为这样 -

private class ListIterator<Type> implements Iterator<Type>

I get a warning in Eclipse, The parameter Type is hiding the type Type 我在Eclipse中收到警告, The parameter Type is hiding the type Type

Why does it complain when I specify the generic type after my class? 为什么我在课后指定泛型类型时会抱怨? Shouldn't I have to do that in order for me to be able to use Type inside my class? 为了让我能够在课堂上使用Type,我不应该这样做吗? I added Type when defining CustomStackUsingArray and that works fine. 我在定义CustomStackUsingArray时添加了Type ,并且工作正常。

When you say class Something<T> (possibly with more than one type parameter), you're telling the compiler that you're defining a new generic class, with T as its parameter. 当你说class Something<T> (可能有多个类型参数)时,你告诉编译器你正在定义一个新的泛型类,以T为参数。 You could have said private class ListIterator<Type2> implements Iterator<Type2> ; 您可以说private class ListIterator<Type2> implements Iterator<Type2> ; that would have been legal, but not what you want. 这本来是合法的,但不是你想要的。 It would have created a new generic class, where Type2 refers to the name of the new type parameter. 它会创建一个新的泛型类,其中Type2引用新类型参数的名称。 When you say private class ListIterator<Type> implements Iterator<Type> , you're actually doing the exact same thing, but with the type parameter named Type instead of Type2 . 当你说private class ListIterator<Type> implements Iterator<Type> ,你实际上做的完全相同,但是使用名为Type而不是Type2的类型参数。 This Type has no connection to the Type of the outer class. Type与外部类的Type无关。 Also, inside this new class, whenever you say Type , you are referring to the new type parameter, which means you can no longer refer to the outer type parameter--it's hidden, which is what Eclipse is telling you. 此外,在这个新类中,每当你说Type ,你指的是new type参数,这意味着你不能再引用外部类型参数 - 它是隐藏的,这就是Eclipse告诉你的。

This all happens because you told the compiler you wanted to create a new generic class. 这一切都发生是因为您告诉编译器您想要创建一个新的泛型类。 But you really don't. 但你真的没有。 You just want an inner class that is part of the outer generic class you're defining (I think). 你只想要一个内部类,它是你定义的外部泛型类的一部分(我认为)。 Therefore, you need to leave off the first <Type> . 因此,您需要取消第一个<Type> And it should work the way you want. 它应该按照你想要的方式工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM