简体   繁体   English

在转换为超类列表时,在Java中取消选中

[英]Unchecked cast in Java when casting to list of superclasses

In my Java program, I need to cast an object of type List<? extends SomeBaseClass> 在我的Java程序中,我需要转换一个List<? extends SomeBaseClass>类型的对象List<? extends SomeBaseClass> List<? extends SomeBaseClass> to List<SomeBaseClass> . List<? extends SomeBaseClass>List<SomeBaseClass> If I cast it directly the compiler complains (shows a warning) that there is an unchecked cast. 如果我直接转换它,编译器会抱怨(显示警告)存在未经检查的强制转换。 Even though I can be sure that all the objects can be converted to their superclass, the compiler does not notice it and reports an unchecked cast. 即使我可以确定所有对象都可以转换为它们的超类,编译器也不会注意到它并报告未经检查的强制转换。 How can I get rid of the warning? 我该如何摆脱警告? Is there any other way than @SupressWarnings("unchecked")? 除了@SupressWarnings(“未选中”)还有其他方法吗?

PS: The code works fine, I am just curious whether there is a better way of doing things. PS:代码工作正常,我只是好奇是否有更好的做事方式。

Edit: 编辑:

Solution: One should only do this type of cast if he is sure the he will not change the list in the future. 解决方案:如果他确定将来不会更改列表,则应该只进行此类投射。 But it is better to cast individual objects when we take them out of the list. 但是当我们将单个对象从列表中取出时,最好将其抛出。

The complier complains since if you add an object of type SomeBaseClass to your List<SomeBaseClass> list, you may "violate" the content of your List<? extends SomeBaseClass> 编译器抱怨,因为如果你将一个SomeBaseClass类型的对象添加到List<SomeBaseClass>列表中,你可能会“违反” List<? extends SomeBaseClass>的内容List<? extends SomeBaseClass> List<? extends SomeBaseClass> list. List<? extends SomeBaseClass>列表。

Here in an example when Number figures as your SomeBaseClass : 这里是一个例子,当Number作为你的SomeBaseClass

List<? extends Number> doubles = new ArrayList<Double>();
List<Number> nums = (List<Number>) doubles;

nums.add(new Integer(5));    // no compiler complaints here...

// doubles now contains an Integer value!

If there is no way around this in your case, I believe the @SuppressWarnings("unchecked") is your best option here. 如果在你的情况下@SuppressWarnings("unchecked")这个问题,我相信@SuppressWarnings("unchecked")是你最好的选择。

What you're trying to do is unsafe. 你要做的是不安全。 Consider this simple example: 考虑这个简单的例子:

import java.util.*;

class SomeBaseClass
{
}

class SomeSubClass extends SomeBaseClass
{
    public static void main(String[] a)
    {
        List<SomeSubClass> orig = new ArrayList<SomeSubClass>();
        // Compiles with no warnings.  This is the purpose of the ? extends syntax
        List<? extends SomeBaseClass> l1 = orig;
        // This is what you're trying to do
        List<SomeBaseClass> l2 = (List<SomeBaseClass>) l1;
        // Then, we add a SomeBaseClass to the new list
        l2.add(new SomeBaseClass());
        // ClassCastException, since this casts a SomeBaseClass to a SomeSubClass
        SomeSubClass first = orig.get(0);
    }
}

You're downcasting, which is a bad practice. 你是沮丧的,这是一种不好的做法。 Try to refactor your code to avoid this casting. 尝试重构代码以避免这种转换。 And of course you should not suppress warnings in this case. 当然,在这种情况下你不应该压制警告。

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

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