泛型类型作为抽象方法的参数? [英] Generic type as a parameter of an abstract method?

查看:378
本文介绍了泛型类型作为抽象方法的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽量把这个问题作为通用的(不是双关语)。

假设我有一个对象车库,它可以作为汽车的容器。它看起来像这样:

  public abstract class Garage {
public abstract List< ;?延伸车> getCars();
public abstract void addCarToGarage(Car car);





$ b因为我很富有,我会有不同类型的车库,包含我的跑车,另一个只有我的SUV等。

因此,我的车库之一将如下所示:

  public class SportCarGarage扩展Garage {
@Override
公开列表< SportCar> getCars(){
//返回卡片的代码
}

@Override
public void addCarToGarage(Car car){
// code to添加汽车到车库
}
}

现在您可能已经看到我的问题了。我可以实现 getCars(),以便它返回 SportCar 列表。但是如果我打电话给 addCarToGarage ,我就需要检查每辆车的类型,如果有人试图在我的SportCarGarage中添加一辆SUV,我将不会收到语法错误。 / p>

我想要这样的东西:

  public abstract class Garage { 
public abstract List <?延伸车> getCars();
public abstract void addCarToGarage(?extends Car car);
}

但是这只会给我一个语法错误。我想有一个简单的解决方案,我只是不知道要搜索什么。希望你能帮上忙。

addCarToGarage 方法中重新使用该类型:

  public abstract class车库< T扩展车> {
public abstract List< T> getCars();
public abstract void addCarToGarage(T car);
}

然后, SportCarGarage 看起来像这样:

  public class SportCarGarage扩展了Garage< SportCar> {
@Override
public List< SportCar> getCars(){
//返回卡片的代码
}

@Override
public void addCarToGarage(SportCar car){
// code to将车加入车库
}
}


I'll try to ask this question as generic (no pun intended) as possible.

Let's say I have an object "garage" that works as a container for cars. It looks something like this:

public abstract class Garage {
    public abstract List<? extends Car> getCars();
    public abstract void addCarToGarage(Car car);
}

Because I am very rich, I will have different type of garages, some only contains my sport cars, another only my SUVs and so on.

So one of my garages will look like this:

public class SportCarGarage extends Garage {
    @Override
    public List<SportCar> getCars() {
        // code to return the cards
    }

    @Override
    public void addCarToGarage(Car car){
        // code to add car to garage
    }
}

Now you may already see my problem. I can implement getCars() so that it will return a list of SportCar. But I would need to check every single car for its type if I call addCarToGarage and I won't get an syntax error if someone tries to add a SUV in my SportCarGarage.

I would want something like this:

public abstract class Garage {
    public abstract List<? extends Car> getCars();
    public abstract void addCarToGarage(? extends Car car);
}

But that will just give me a syntax error. I guess there is an easy solution for that and I just don't know what to search for. Hope you can help.

解决方案

You can make the Garage generic and re-use the type in the addCarToGarage method:

public abstract class Garage<T extends Car> {
    public abstract List<T> getCars();
    public abstract void addCarToGarage(T car);
}

Then, the SportCarGarage would look like this:

public class SportCarGarage extends Garage<SportCar> {
    @Override
    public List<SportCar> getCars() {
        // code to return the cards
    }

    @Override
    public void addCarToGarage(SportCar car){
        // code to add car to garage
    }
}

这篇关于泛型类型作为抽象方法的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆