Java 避免未检查的呼叫警告

Java 避免未检查的呼叫警告,java,generics,hashmap,Java,Generics,Hashmap,我有以下代码(部分代码) 希望代码是清楚的。 Vehicle是超级级,它只包含关于汽车的更多细节。 车库类假设在一个hashmap中容纳所有的汽车。 有两种类型的汽车,提及规范的私家车和未提及规范的LeesingVehicle,都是车辆的子类 当我尝试使用javac-Xlint:unchecked*.java编译它时,我得到以下结果 Main.java:79: warning: [unchecked] unchecked call to add(T) as a member of the raw

我有以下代码(部分代码)

希望代码是清楚的。 Vehicle是超级级,它只包含关于汽车的更多细节。 车库类假设在一个hashmap中容纳所有的汽车。 有两种类型的汽车,提及规范的私家车和未提及规范的LeesingVehicle,都是车辆的子类

当我尝试使用javac-Xlint:unchecked*.java编译它时,我得到以下结果

Main.java:79: warning: [unchecked] unchecked call to add(T) as a member of the raw type Garage
                        CarsGarage.add(PrivateCar);
                                      ^
  where T is a type-variable:
    T extends Vehicle declared in class Garage
Main.java:97: warning: [unchecked] unchecked call to add(T) as a member of the raw type Garage
                        CarsGarage.add(LeasedCar);
                                      ^
  where T is a type-variable:
    T extends Vehicle declared in class Garage
Main.java:117: warning: [unchecked] unchecked conversion
                    CarsList = CarsGarage.getAll();
                                                ^
  required: ArrayList<Vehicle>
  found:    ArrayList
3 warnings
Main.java:79:warning:[unchecked]unchecked调用将(T)添加为原始类型的成员
加上(私家车);
^
其中T是一个类型变量:
T级车库内申报的车辆
java:97:警告:[未选中]未选中将(T)添加为原始类型的成员的调用
车辆收费。添加(租赁车辆);
^
其中T是一个类型变量:
T级车库内申报的车辆
Main.java:117:警告:[未选中]未选中的转换
CarsList=CarsGarage.getAll();
^
必需:ArrayList
发现:ArrayList
3警告
如何避免这种警告

谢谢

Garage CarsGarage = new Garage(20);
这里您没有为
Garage
指定类型参数,它实际上是一个泛型类
Garage
。你需要:

Garage<Vehicle> CarsGarage = new Garage<Vehicle>(20);
Garage CarsGarage=新车库(20);

你是说像车库汽车车库=新车库(20)?编译器如何判断T是什么?更像是Garage CarsGarage=new Garage(20)。你以前看过泛型吗?Sum/Oracle的教程非常棒。很好,非常感谢,对Java和泛型来说有点陌生,谢谢。
Garage CarsGarage = new Garage(20);
Garage<Vehicle> CarsGarage = new Garage<Vehicle>(20);