Java 泛型 - 为什么此方法调用被视为未经检查的转换
以下代码示例编译,但带有编译器警告
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class SubArrayList<T> extends ArrayList{}
class ZiggyTest2{
public static void main(String[] args){
ArrayList<Animal> nums = new SubArrayList<Animal>();
}
public static void testMethod(ArrayList<Animal> anim){
System.out.println("In TestMethod");
}
}
当我编译上面的内容时,我收到以下警告
Note: ZiggyTest2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
或者如果我使用 -Xlint:unchecked 编译它,我收到以下警告
ZiggyTest2.java:12: warning: [unchecked] unchecked conversion
found : SubArrayList<Animal>
required: java.util.ArrayList<Animal>
ArrayList<Animal> nums = new SubArrayList<Animal>();
^
1 warning
如果我将 nums 的初始化更改为
List<Animal> nums = new ArrayList<Animal>();
,那么我不会收到任何警告。
为什么他们的行为不同。 ArrayList 是 List 的子类型,而 SubArrayList 是 ArrayList 的子类型,因此我希望实例化是相同的。
谢谢。
编辑
此外,如果我调用该方法并向其传递一个一般初始化的引用,它也会产生一个警告。
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class SubArrayList<T> extends ArrayList{}
class ZiggyTest2{
public static void main(String[] args){
SubArrayList<Animal> nums = new SubArrayList<Animal>();
testMethod(nums);
}
public static void testMethod(ArrayList<Animal> anim){
System.out.println("In TestMethod");
}
}
我认为只有当您将通用代码与非通用代码混合时才会出现编译器警告。
The following code examples compiles but with a compiler warning
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class SubArrayList<T> extends ArrayList{}
class ZiggyTest2{
public static void main(String[] args){
ArrayList<Animal> nums = new SubArrayList<Animal>();
}
public static void testMethod(ArrayList<Animal> anim){
System.out.println("In TestMethod");
}
}
When i compile the above i get the following warning
Note: ZiggyTest2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Or if i compile it with -Xlint:unchecked i get the following warning
ZiggyTest2.java:12: warning: [unchecked] unchecked conversion
found : SubArrayList<Animal>
required: java.util.ArrayList<Animal>
ArrayList<Animal> nums = new SubArrayList<Animal>();
^
1 warning
If i change the initialisation of nums to
List<Animal> nums = new ArrayList<Animal>();
then i dont get any warnings.
Why are they behaving differently. ArrayList is a subtype of List and SubArrayList is a subtype of ArrayList so i was expecting the instantiation to be the same.
Thanks.
Edit
Also if i call the method and pass it an reference initialised generically it also produces a warning.
class Animal{}
class Dog extends Animal{}
class Cat extends Animal{}
class SubArrayList<T> extends ArrayList{}
class ZiggyTest2{
public static void main(String[] args){
SubArrayList<Animal> nums = new SubArrayList<Animal>();
testMethod(nums);
}
public static void testMethod(ArrayList<Animal> anim){
System.out.println("In TestMethod");
}
}
I thought compiler warnings will only appear if you mix generic code with non-generic code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
SubArrayList
类仅扩展ArrayList
(本质上是ArrayList
),而不是ArrayList
code>,未选中从SubArrayList
到ArrayList
的转换。如果将其更改为
class SubArrayList;扩展 ArrayList; {}
,您将不会再遇到问题了:)Your
SubArrayList<T>
class only extendsArrayList
(essentiallyArrayList<?>
), and notArrayList<T>
, making a cast fromSubArrayList<T>
toArrayList<T>
unchecked.If you change it to
class SubArrayList<T> extends ArrayList<T> {}
, you will not have the problem anymore :)将
添加到您的 ArrayList 中,如下所示Add a
<T>
to your ArrayList as inList
是一个接口,而不是一个类。 ArrayList 不是 List 的子类,只是一个实现 List 接口的类。然而,SubArrayList 是 ArrayList 的子类。List
is an interface, not a class. ArrayList isn't a subclass of List, simply a class that implements the List interface. SubArrayList is, however, a subclass of ArrayList.