我有以下代码:
简单类
public class Transport { }
public class Car : Transport { }
public class Bus : Transport { }
包装类
public class Wrapper<T> where T : Transport
{
public T Vehicle { get; set; }
public void Brake() { }
public void Accelerate() { }
}
public class BusWrapper : Wrapper<Bus>
{
public void PickupPassenger() { }
}
public class CarWrapper : Wrapper<Car>
{
public void GoRacing() { }
}
这就是我正在尝试的,它给了我编译错误:
List<Wrapper<Transport>> list = new List<Wrapper<Transport>>();
list.Add(new CarWrapper());
list.Add(new BusWrapper());
list.ForEach(wrapper =>
{
wrapper.Accelerate();
if (wrapper is CarWrapper)
((CarWrapper)wrapper).GoRacing();
else if (wrapper is BusWrapper)
((BusWrapper)wrapper).PickupPassenger();
wrapper.Brake();
});
错误
cannot convert from 'CarWrapper' to 'Wrapper<Transport>'
cannot convert from 'BusWrapper' to 'Wrapper<Transport>'
问题
我在这里做错了什么,我想在这里实现的任何解决方法。
提前致谢。