daimond括号在包含两种类型的java泛型列表中的含义是什么

daimond括号在包含两种类型的java泛型列表中的含义是什么,java,generics,Java,Generics,什么是列表?列表中两个参数的含义是什么?我是泛型新手,这是我在网上选择的示例代码…请解释 package generics; import java.util.ArrayList; import java.util.List; class Car<T>{ private T t; public T getT() { return t; } public void setT(T t) { this.t = t;

什么是列表?列表中两个参数的含义是什么?我是泛型新手,这是我在网上选择的示例代码…请解释

package generics;
import java.util.ArrayList;
import java.util.List;

class Car<T>{
    private T t;

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}
public class CarList{
    public static <U> void addCar(U u, List<Car<U>> cars){
        //Whats is List<Car<U>>?,what is the meaning of two parameters inside the List?

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

        car.setT(u);
        //Could someone expalin the above code

        cars.add(car);
    }
    public static void main(String[] args) {
        ArrayList<Car<String>> carlist=new ArrayList<>();
        CarList.addCar("Audi", carlist);
    }
}

汽车本身是一个广义类——U指的是它,而不是列表。List应该解释为U的Car列表。

首先看一下Car类,它允许您泛化该类,并将相同类型的内容设置/获取到该类。想象一下,如果你想给你的汽车设置一个引擎,那可能是一个糟糕的环境。由于不同的发动机以不同的方式工作,汽油、柴油、骡子拉动汽车,但都会启动汽车,因此您希望有一个接口,以便在以后有汽油、柴油和鲻鱼发动机的具体实现:

interface Engine{
    public void start();
}
并说上述接口的第一个实现:

class GasEngine implements Engine{
    public void start(){
        // check if enoth fuel available
        // start ignition
        // etc. etc. 
    }
}
由于您的Car类是用泛型定义的,因此第一个示例将解释为:

public class TestMe{
    public static void main(String[] args) {

        // You just create "a Car" without any more information
        Car car = new Car();
        // Since there was no additional Information you can apply anything to that Car
        car.setT("Anything");
        car.setT(new Integer(5));

        // You create a Car "bound" to the Engine-Interface
        Car<Engine> carGeneral = new Car<Engine>();

        // So only references that are implementing the Engine-Interface can be set to this car
        GasEngine myGasEngine = new GasEngine();
        carGeneral.setT(myGasEngine);

        // While this wont compile:
        // carGeneral.setT("Anything");

        // Also note your car is not an Engine!!!
        System.out.println("A Car is an Engine, true of false? -> "+ (carGeneral instanceof Engine));

    }
}
现在我们到了我担心的地步,我的例子是不好的,因为我完全不知道“U”可以是什么来给这个代码任何我首先想到的汽车类型的意义

让我们做一个最小的更改,并说您的方法不是“addCar”,而是“CreateCarWithEngineeAndToCarList”。在这种情况下,它将如下所示:

class CarList{
public static <U> void createCarWithEngineAndAddToCarList(U u, List<Car<U>> cars){

    /* Q: Whats is List<Car<U>>?,what is the meaning of two parameters inside the List?
     * 
     * A: "They define the List cars is only able to hold objects of the Type <Car<U>>"
     * 
     * A: At this point we know:
     * 
     *    - cars is of type List (has Methods such as 'add')
     *    - cars contains Objects of Type Car<U>
     *      => No matter what we take out of that list, it will be of type Car<U>
     *    - The type <U> is not yet known and be defined at runtime
     */

    // We create a new Car-Object 
    Car<U> car=new Car<U>();

    // We set the engine 
    car.setT(u);

    // We add the new car to the list of existing cars
    cars.add(car);
}
}

虽然我希望进一步澄清这两个参数,但上述方法仍然不好。因为我们“知道”我们的“U”将是Engine类型,所以我们希望通过该方法直接指定它。但是,这是您使用它的方式:

 public class TestMe{
    public static void main(String[] args) {

            GasEngine myGasEngine = new GasEngine();

            ArrayList<Car<Engine>> carlist = new ArrayList<Car<Engine>>();

            CarList.createCarWithEngineAndAddToCarList(myGasEngine, carlist);
        }
    }

Brb。老板来了

你给答案加了标签。在泛型中,列表中的U是一个类型占位符。这意味着您可以使用任何类型来代替U.@Terminator,然后将其标记为接受的绿色复选标记。你也可以投票选出有用的答案。