gpt4 book ai didi

java - 了解内部泛型类

转载 作者:搜寻专家 更新时间:2023-10-31 08:16:45 24 4
gpt4 key购买 nike

我写了下面的代码:

public class Test<T> {

public void method(){
B b = new B();
}

public class B{ }
}

//Some method in some class contains the following lines
Test<Integer> t = null;
Test.B b = t.new B(); //warning Test.B is a raw type

为什么我会收到此警告?内部类型 B 的声明不包含类型参数,因此它不是泛型类型。此外,the specification给我们以下内容:

A class is generic if it declares one or more type variables

B 类没有声明类型变量。那为什么是泛型呢?

最佳答案

虽然内部类 B 没有声明任何类型变量,但它的一个实例隐式引用了外部类的一个实例,而外部类声明了。

Why did I get this warning?

Test<Integer> t = null;
Test.B b = t.new B(); //warning Test.B is a raw type

因为您使用原始类型声明了变量 b。相反,您可以声明:

Test<Integer>.B b = t.new B(); // no warning!

关于java - 了解内部泛型类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com