【发布时间】:2009-05-15 11:17:28
【问题描述】:
我有两个类和一个类似的接口
interface IVehicle
{
void Drive();
}
class Benz :IVehicle
{
public void Drive()
{
Console.WriteLine("WOW! driving benz");
}
}
class Ferrari : IVehicle
{
public void Drive()
{
Console.WriteLine("WOW! driving ferrari");
}
}
我有一个使用它的 Driver 类。
class Driver
{
public void StartDriving(IVehicle vehicle)
{
vehicle.Drive();
}
}
还有一个通用的驱动程序。
class GenericDriver
{
public void StartDriving<T>() where T : IVehicle , new()
{
T vehicle = new T();
vehicle.Drive();
}
}
问题
- 与普通驱动程序相比,您认为通用实现有什么优势吗?如果是,它们是什么?
- 你更喜欢哪一个?通用的还是普通的?
- 有没有更好的方法来实现通用驱动程序?
- 我感觉 C# 中的泛型与 C++ 模板相比非常有限。这是真的吗?
有什么想法吗?
【问题讨论】: