假設我有兩個類,并且我想創建一個方法來處理來自兩個類的資料,我該如何設定一個可以同時接受這兩個類的方法,我可以將它們創建為它們型別的物件?
Public classs Car:Wheel
{
public Color color {get; set;}
}
Public class Bike:Wheel
{
public bool hasRadio {get ; set;}
public Color color {get; set;}
}
Public class Wheel
{
public WheelRimType {get; set;}
public int WheelSize {get; set;}
}
我想動態地將汽車或自行車類傳遞給方法。在該方法中如何將其轉換為正確的物件,以便我不必為每個物件撰寫兩次相同的方法?
Car c = new Car();
Bike b = new Bike();
ValidateVehichle(c);
private void ValidateVehicle(Type T??)
{
//convert to either car or bike here
}
uj5u.com熱心網友回復:
您的汽車和自行車“有”輪子,但不是“輪子”,因此在考慮繼承時,您可能需要閱讀更多關于“has-a-is-a”的術語:has-a-is-a-terminology-in-面向物件語言
我相信你會更好地為 Car and Bike 創建一個基類,并在這個基類中放置一個 Validation 方法,然后可以在 Car and Bike 中實作它。類似于以下內容:
基類
VehicleBase將在 type 的“has-a”屬性之后包含 Wheels 屬性Wheel,在示例中 aVehicleBase將包含IEnumerable<T>whereTisWheel,其中車輛可以有一個或多個輪子。
public abstract class VehicleBase
{
public abstract bool Validate();
public Color color { get; set;}
public IEnumerable<Wheel> Wheels {get; set;}
}
public class Car : VehicleBase
{
public override bool Validate()
{
//do Car validation
}
}
public class Bike : VehicleBase
{
public bool hasRadio {get ; set;}
public override bool Validate()
{
//do Bike validation
}
}
public class Wheel
{
public WheelRimType { get; set;}
public int WheelSize {get; set;}
}
uj5u.com熱心網友回復:
兩個經典的可能性
class Vehicle{
}
class Car : Vehicle{
}
class Bike : Vehicle{
}
那么你可以做
private void ValidateVehicle(Vehicle v)
{
//convert to either car or bike here
}
進行驗證的慣用方式實際上將具有以下內容
abstract class Vehicle{
virtual bool Validate();
}
class Car : Vehicle{
override bool Validate(){
}
}
class Bike : Vehicle{
override bool Validate(){
}
}
你會做的vobj.Validate()
其中 vobj 是車輛的一個實體
也可以通過界面實作
interface IVehicle{
}
class Car : IVehicle{
}
class Bike : IVehicle{
}
和
private void ValidateVehicle(Vehicle v)
{
//convert to either car or bike here
}
uj5u.com熱心網友回復:
使用泛型:
ValidateVehichle<Car>(car);
private void ValidateVehicle<T>(T vehicle)
{
// no convertion needed. use T as the type of the object
T vehicle2 = vehicle;
.....
}
有關泛型的更多資訊:https ://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/generics
uj5u.com熱心網友回復:
您可以使用泛型并執行以下操作:
void Main()
{
Car car = new Car();
car.color = Color.Black;
car.WheelSize = 22;
ValidateVehicle<Car>(car)
}
private void ValidateVehicle<T>(T vehicle)
{
// You still need to determine what time this object is
T someVehicle = vehicle;
Console.WriteLine("This is a generic method");
}
您也可以選擇使用多載方法。您可以使用一個具有無限多載的方法名稱,如下所示:
void Main()
{
Car car = new Car();
car.color = Color.Black;
car.WheelSize = 22;
Bike bike = new Bike();
bike.color = Color.Red;
bike.hasRadio = false;
bike.WheelSize = 12;
ValidateVehicle(car);
ValidateVehicle(bike);
}
// You can define other methods, fields, classes and namespaces here
public class Car: Wheel
{
public Color color { get; set; }
}
public class Bike : Wheel
{
public bool hasRadio { get; set; }
public Color color { get; set; }
}
public class Wheel
{
public int WheelSize { get; set; }
}
private void ValidateVehicle(Car car)
{
Console.WriteLine("This is your car method");
}
private void ValidateVehicle(Bike bike)
{
Console.WriteLine("This is your bike method");
}
輸出是:
This is your car method
This is your bike method
uj5u.com熱心網友回復:
典型的解決方案是引入一個通用介面,以便ValidateVehicle可以使用任何一種型別:
public interface IVehicle{
Color color {get; set;}
}
public class Car:Wheel, IVehicle {
public Color color {get; set;}
}
public class Bike:Wheel, IVehicle
{
public bool hasRadio {get ; set;}
public Color color {get; set;}
}
private void ValidateVehicle(IVehicle vehicle)
{
// Implementation here
}
這顯然需要IVehicle公開所有使用的方法和屬性ValidateVehicle。
如果汽車和自行車的驗證邏輯不同,您應該將驗證方法放在您的車輛型別上。這樣,每輛車都可以以自己的方式進行驗證。
public interface IVehicle{
Color color {get; set;}
bool ValidateVehicle();
}
第三種方法是使用型別檢查:
private void ValidateVehicle(object vehicle)
{
if(vehicle is Car car){
...
}
if(vehicle is Bike bike){
...
}
}
但這并不容易維護。假設你引入了一輛摩托車,如果你使用一個介面,你只需要讓你的新車繼承那個介面,如果你犯了任何錯誤,編譯器就會抱怨。如果您使用型別檢查,您需要記住檢查型別的所有不同位置并為您的摩托車添加另一個檢查,這很快就會成為維護的噩夢。由于這種型別的檢查通常不受歡迎。
我猜這是為了分配,但我不喜歡這樣的面向物件的例子。值得注意的是,汽車不是輪子,它有輪子,即輪子應該是財產而不是繼承。
在演示面向物件時,我通常更喜歡幾何,它有很多組合和繼承的示例,并且在實際程式中實際上很有用。相比之下,我很難想象車輛繼承層次結構在哪里真正有用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/480506.html
上一篇:SQL聯接在Powershell腳本中不起作用。投擲錯誤
下一篇:如何將游戲物件轉換為轉換變數
