gpt4 book ai didi

java - java中的钻石手链是什么意思泛型包含两种类型List>

转载 作者:太空宇宙 更新时间:2023-11-04 14:29:16 26 4
gpt4 key购买 nike

什么是 List<Car<U>> ?,列表中的两个参数的含义是什么?我是泛型新手,这是我在网上挑选的示例代码...请解释一下!

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);
}
}

最佳答案

首先看一下 Car 类,它允许您概括该类并为该类设置/获取相同类型的内容。想象一下(这可能是一个糟糕的例子)您想要为您的汽车设置一个引擎。由于不同的发动机以不同的方式工作(汽油、柴油、拉车的骡子),但所有发动机都使汽车“启动”,因此您需要一个接口(interface)以便稍后具体实现汽油、柴油和鲻鱼发动机:

interface Engine{
public void start();
}

并说出上述接口(interface)的第一个“实现”:

class GasEngine implements Engine{
public void start(){
// check if enoth fuel available
// start ignition
// etc. etc.
}
}

由于您的 Car-Class 是使用泛型定义的,因此第一个示例将解释为:

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”,而是“createCarWithEngineAndAddToCarList”。在这种情况下,它看起来像这样:

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”将属于类型引擎,所以我们希望通过方法直接指定它。然而,这就是您使用它的方式:

 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。老大来了

关于java - java中的钻石手链是什么意思泛型包含两种类型List<car<u>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26336683/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com