[英]Passing in a sub-class to a method but having the super class as the parameter?
I have an abstract class Vehicle
with 2 implemented subclasses RedVehicle
and YellowVehicle
.我有一个抽象类
Vehicle
其中包含 2 个实现的子类RedVehicle
和YellowVehicle
。
In another class I have a List<Vehicle>
containing instances of both subclasses.在另一个类中,我有一个
List<Vehicle>
包含两个子类的实例。 I want to be able to pass into a method a class type and then use that type to decide which set of objects I want to do something to in the List
.我希望能够将一个类类型传递给一个方法,然后使用该类型来决定我想要对
List
中的哪一组对象执行某些操作。
Since Class
is generic I should parameterise it with something, however putting the parameter as the parent class Vehicle
stops the calling code working since exampleMethod
is now expecting a type of Vehicle, not a subclass of RedVehicle
or YellowVehicle
.由于
Class
是通用的,我应该用一些东西对其进行参数化,但是将参数作为父类Vehicle
停止调用代码的工作,因为exampleMethod
现在需要一种 Vehicle 类型,而不是RedVehicle
或YellowVehicle
的子类。
I feel there should be a clean way to do this so what would be the correct way to implement the functionality?我觉得应该有一种干净的方法来做到这一点,那么实现该功能的正确方法是什么?
nb I don't necessarily have to pass in the Class
type, if there are better suggestions I'd be happy to try those. nb 我不一定必须传入
Class
类型,如果有更好的建议,我很乐意尝试。
Calling code:调用代码:
service.exampleMethod(RedVehicle.class);
service.exampleMethod(YellowVehicle.class);
Fields/Method:字段/方法:
//List of vehicles
//Vehicle has 2 subclasses, RedVehicle and YellowVehicle
private List<Vehicle> vehicles;
//Having <Vehicle> as the Class parameter stops the calling code working
public void exampleMethod(Class<Vehicle> type)
{
for(Vehicle v : vehicles)
{
if(v.getClass().equals(type))
{
//do something
}
}
}
改为这样做:
public <T extends Vehicle> void exampleMethod(Class<T> type)
Why don't you use the visitor pattern ?为什么不使用访问者模式?
That way you这样你
if(v.getClass().equals(type))
)if(v.getClass().equals(type))
) In detail:详细地:
your abstract class Vehicle
gets a method accept(Visitor v)
, with the subclasses implementing it by calling the appropriate method on v
.您的抽象类
Vehicle
获得一个方法accept(Visitor v)
,子类通过在v
上调用适当的方法来实现它。
public interface Visitor {
visitRedVehicle(RedVehicle red);
visitYellowVehicle(YellowVehicle yellow);
}
Using a visitor:使用访问者:
public class Example {
public void useYellowOnly() {
exampleMethod(new Visitor() {
visitRedVehicle(RedVehicle red) {};
visitYellowVehicle(YellowVehicle yellow) {
//...action
});
}
public void exampleMethod(Visitor visitor){
for(Vehicle v : vehicles) {
v.accept(visitor);
}
}
}
The accepted answer works and got me where I wanted to go.接受的答案有效并让我到达了我想去的地方。 I thought I would add this just to make it clearer to anyone who might need it.
我想我会添加这个只是为了让任何可能需要它的人更清楚。
In this case RevisedExposure is a sub-class of Exposure.在这种情况下,RevisedExposure 是 Exposure 的子类。 I need to call GetMetadata() with a list of either of these, which results in the same result set.
我需要使用其中之一的列表调用 GetMetadata(),这会产生相同的结果集。
private async Task<List<Metadata>> GetMetadata<T>(List<T> exposures) where T : Exposure
Now I can call this method from two places with different versions of the list like this.现在我可以像这样从两个不同版本的列表调用这个方法。
var metadata = await GetExposureMetadata(revisions);
or要么
var metadata = await GetExposureMetadata(exposures);
works great!效果很好!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.