Java:获取泛型的实例

发布于 2024-12-04 03:53:34 字数 1543 浏览 1 评论 0原文

可能的重复:
通过反射获取java中泛型参数的类型

您好,我在这里阅读了有关获取泛型类型实例的解决方案。我认为我的问题有点不同,所以为了简单起见,我将其分解。 假设我有三个类:

public class Car (){
    public int velocity;
    public String name;
    public Car(int velocity, String name){
        this.velocity = velocity;
        this.name = name;
    }
}

public class ColoredCar extends Car(){
    public String color;
    //setters and getters for color, same Constructor....
}
public class DrivenCar extends ColoredCar(){
    public Driver driver;
    //setters and getters, same Constructor
}

现在我扩展了 ArrayList,如下所示:

public class Cars<CAR extends Car> extends ArrayList<CAR>{

    public String[] getColors(){
        String[] ret = new String[this.size()];
        int count = 0;
        for(CAR c: this){
            ret[count++] = c.getColor();
        }
        return ret;
    }
    //some more.....
}

好的,如果我需要创建一个扩展 Cars 的类,并且在其构造函数中,我已经知道它们是 CAR< 的 10 个对象/code>,我有什么可能性?我知道我可以创建具有速度和名称的汽车对象,这是我所拥有的。

编辑: 假设我的汽车中有一个函数:

public CAR createCar(int velocity, String name){
    return (CAR)new Car(velocity, name); 
}

并且在运行时汽车被定义为汽车,我是否会收到错误,因为我无法将汽车转换为 DrivenCar?

Possible Duplicate:
get type of a generic parameter in java with reflection

Hi I read here about solutions to get instances of generic types. I think my my problem differs a bit, so I break it down for easyness.
Lets assume I have three classes:

public class Car (){
    public int velocity;
    public String name;
    public Car(int velocity, String name){
        this.velocity = velocity;
        this.name = name;
    }
}

public class ColoredCar extends Car(){
    public String color;
    //setters and getters for color, same Constructor....
}
public class DrivenCar extends ColoredCar(){
    public Driver driver;
    //setters and getters, same Constructor
}

Now I extended the ArrayList like:

public class Cars<CAR extends Car> extends ArrayList<CAR>{

    public String[] getColors(){
        String[] ret = new String[this.size()];
        int count = 0;
        for(CAR c: this){
            ret[count++] = c.getColor();
        }
        return ret;
    }
    //some more.....
}

ok, If I need to create a class which extends Cars and in its Constructor I know already they are 10 Objects of CAR, what do I have as possibility? I know I can create Car Objects with velocity and name, which I have.

Edit:
lets say I have a function in my Cars:

public CAR createCar(int velocity, String name){
    return (CAR)new Car(velocity, name); 
}

and at Runtime Cars is defined as Cars, do I get an error, because I can't cast a Car to a DrivenCar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2024-12-11 03:53:34

如果您询问如何在运行时构造对象 T,您始终可以传入 T.class,然后使用其 newInstance() 方法创建该类型的对象。如果您询问如何使用 T 对象列表初始化容器类,那么...

您可以定义一个构造函数,该构造函数使用 super 来调用父构造函数并传入项目(转换为首先是一个集合),或者您可以定义自己的构造函数,该构造函数只需调用 add 方法来添加您的十辆车。然而,这是对继承模式的严重误用。

继承不是为了代码重用。它是为了变化(即同一接口的多种不同的可能实现)。对于代码重用,包含几乎总是更好的范例。 IE:

class Cars<CAR> {
    private List<CAR> cars = new ArrayList<CAR>();

    // ...

    public Iterable<String> getColors() {
       return Iterables.transform(cars, getCarToColorTransform());
    }

    // Expose other methods of List<CAR>, but ONLY the ones you need.
}

If you are asking about how to construct an object T at runtime, you can always pass in T.class, and then use its newInstance() method to create objects of that type. If you are asking about how to initialize your container class with the list of T objects, then...

You could define a constructor that uses super to invoke a parent constructor and pass in the items (converted to a collection, first), or you could define your own constructor that simply calls the add method to add your ten cars. However, this is a serious misuse of the inheritance pattern.

Inheritance is NOT for code reuse. It is for variation (i.e. multiple different possible implementations of the same interface). For code reuse, containment is almost always a better paradigm. I.e.:

class Cars<CAR> {
    private List<CAR> cars = new ArrayList<CAR>();

    // ...

    public Iterable<String> getColors() {
       return Iterables.transform(cars, getCarToColorTransform());
    }

    // Expose other methods of List<CAR>, but ONLY the ones you need.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文