我有一个类有 9 个不同的属性,每个属性都是一个类
public class Vehicles
{
Car car; //class
Train train; //class
Plane plane; //class
}
我将这个 Vehicle 对象传递给一个方法
例如
var Vehicles = new Vehicles();
Vehicles.Car = new Car()
Object1.WorkOutTransport(vehicle)
我需要在 Object1 中做的是在不使用 switch 语句的情况下实例化“车辆”并检查其他语句是否为空
这不是“家庭作业问题”...我已将其简化以仅说明问题
实际的车辆类有 9 个可以实例化的可能类
最佳答案
我建议重新考虑您的设计。
为什么不让所有车辆类型都实现一个通用接口(interface) IVehicle
,然后让您的 Vehicles
类具有一个名为 Vehicle
的属性。
您只需担心一个属性。
public Interface IVehicle
{
... //Properties Common to all vehicles
}
public class Car : IVehicle
{
... //Properties to implement IVehicle
... //Properties specific to Car
}
public class Vehicles
{
public IVehicle Vehicle { get; set; }
}
var vehicles = new Vehicles();
vehicles.Vehicle = new Car();
... //Do whatever else you need to do.
关于c# - 我怎样才能得到哪个类已传递给方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13812874/