作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我写了下面的代码:
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/
我是一名优秀的程序员,十分优秀!