为什么“&不兼容的类型&"使类通用时,编译错误会变成警告吗? [英] Why does an "Incompatible types" compile error turn into a warning when a class is made generic?

查看:59
本文介绍了为什么“&不兼容的类型&"使类通用时,编译错误会变成警告吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码,无法编译:

  import java.util.ArrayList;导入java.util.List;公共类TypeSafetyTest {公共静态void main(String [] args){TypeSafetyTest测试=新的TypeSafetyTest();测试运行();}私人无效run(){Car car = new Car();List< String>wheelWeights = car.getWheelWeights();}私家车{List< Double>wheelWeights = new ArrayList< Double>();公共列表< Double>getWheelWeights(){return wheelWeights;}public void setWheelWeights(List< Double< wheelWeights){this.wheelWeights = wheelWeights;}}} 

它在行上给出不兼容的类型"错误:

  List< String>wheelWeights = car.getWheelWeights(); 

但是,如果我更改行:

 私人舱车{ 

 私有类Car< T>{ 

然后,代码将成功编译,并在以前出现编译错误的行上显示警告未检查的分配".为什么会这样呢?我期望这两种情况下都会产生编译错误.

解决方案

我看到的一个问题是:

 公共列表< Double>getWheelWeights(){return wheelWeights;} 

返回 Double 类型的 List ,但是您将其分配给 List< String>

更改 Car< T> 时未看到编译错误的原因是,因为您的实例创建是原始类型,这告诉编译器在编译时忽略类型安全,原因是向后兼容.

  Car< Object>car = new Car< Object>(); 

如果您这样更改对象,则将看到编译器再次抛出该消息.

请参考 JLS 4.8 有关更多信息.

以下是JLS的示例:

 类外部< T> {内部类{s;}}外·内< Double>x = null;//非法的 

不可能以部分原始类型(稀有"类型)访问Inner,因为Outer本身是原始的,因此包括Inner在内的所有内部类也是原始的,因此无法传递任何类型参数内心

仅允许出于对遗留代码兼容性的考虑而使用原始类型.强烈反对在将泛型引入Java编程语言之后在代码中使用原始类型..将来的Java编程语言版本可能会禁止使用原始类型.>

我建议阅读泛型(已更新)

I have this code, which fails to compile:

import java.util.ArrayList;
import java.util.List;

public class TypeSafetyTest {

    public static void main(String[] args) {
        TypeSafetyTest test = new TypeSafetyTest();
        test.run();
    }

    private void run() {
        Car car = new Car();
        List<String> wheelWeights = car.getWheelWeights();
    }

    private class Car {
        List<Double> wheelWeights = new ArrayList<Double>();

        public List<Double> getWheelWeights() {
            return wheelWeights;
        }

        public void setWheelWeights(List<Double> wheelWeights) {
            this.wheelWeights = wheelWeights;
        }
    }
}

It gives an "Incompatible types" error on the line:

List<String> wheelWeights = car.getWheelWeights();

However, if I change the line:

private class Car {

to

private class Car<T> {

then the code compiles successfully with the warning "Unchecked assignment" on the line that used to have the compile error. Why is this so? I was expecting it to give a compile error in both cases.

解决方案

One issue I see is:

 public List<Double> getWheelWeights() {
            return wheelWeights;
        }

returns Double type List, but you are assigning it to List<String>

The reason why you are not seeing compile error when you change Car<T> is, because your instance creation is raw type, which is telling compiler that ignore type safety at compile time, due to backward compatability.

Car<Object> car = new Car<Object>();

If you change your object like this, then you will see compiler throwing the message again.

Please refer JLS 4.8 for more information.

Here is example from JLS:

    class Outer<T>{
        class Inner<S> {
            S s;
        }
    }

Outer.Inner<Double> x = null;  // illegal

It is not possible to access Inner as a partially raw type (a "rare" type), because Outer itself is raw, hence so are all its inner classes including Inner, and so it is not possible to pass any type arguments to Inner

The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of generics into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.

I would suggest reading Generics (Updated)

这篇关于为什么“&amp;不兼容的类型&amp;"使类通用时,编译错误会变成警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆