什么是List<Car<U>>
?, List中两个参数的含义是什么?对于泛型来说是新手,这是我在线选择的示例代码...解释一下!
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);
}
}
答案 0 :(得分:0)
Car
本身就是一个广义类 - U
引用它,而不是List
。 List<Car<U>>
应解释为&#34; U&#34;。
答案 1 :(得分:0)
首先看一下Car类,它允许你对类进行概括,并为该类设置/获取相同类型的东西。想象一下(这可能是一个糟糕的例子)你想要将你的汽车设置为引擎。由于不同的发动机以不同的方式工作(汽油,柴油,骡子拉车),但所有汽车都开始了#34;开始&#34;你想要一个接口,以便以后有你的燃气,柴油和m鱼的具体实施 - 发动机:
interface Engine{
public void start();
}
说出第一个&#34;实施&#34;上面的界面:
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));
}
}
现在我们处于我担心的地步,我的榜样很糟糕,因为我绝对没有想到什么&#39; U&#39;可能是给这个代码任何意义(我首先考虑了汽车类型?...)。
让我们做一个小改动,并说你的方法没有被调用&#39; addCar&#39;但是&#39; createCarWithEngineAndAddToCarList&#39;。在这种情况下,它看起来像这样:
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);
}
}
虽然我希望澄清&#34;两个参数&#34;更多一点,上面的方法仍然是&#34;坏&#34;。因为我们知道&#39;我们的&#39; U&#39;将是Type 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。老板在这里