[英]Generic type for property of an Interface
我有一組如下所示的數據結構:
IDataPoint
接口的不同數據點(以確保值和坐標點的存在)ITuple
(以確保至少存在一個 (X) 坐標) 我的問題是,我還沒有找到如何使IDataPoint
的Coord
屬性通用,以便坐標可以是 2d 或 3d(或者以后需要的話)。 這是我的嘗試:
public interface IDataPoint
{
float Value { get; }
<Tuple> Coords { get; } where <ITuple> : ITuple
string ToString();
}
我的錯誤在哪里,或者這根本不可能?
其余代碼
public interface ITuple<T>
{
T X { get; }
string ToString();
}
public struct TwoTuple<T> : ITuple<T>
{
public T X { get; }
public T Y { get; }
public TwoTuple(T x, T y)
{
X = x;
Y = y;
}
public override string ToString()
{
return "(" + X + ", " + Y + ")";
}
}
public struct ThreeTuple<T> : ITuple<T>
{
public T X { get; }
public T Y { get; }
public T Z { get; }
public ThreeTuple(T x, T y, T z)
{
X = x;
Y = y;
Z = z;
}
public override string ToString()
{
return "(" + X + ", " + Y + ", " + Z + ")";
}
}
public interface IDataPoint
{
float Value { get; }
<Tuple> Coords { get; } where <ITuple> : ITuple
string ToString();
}
public struct BarDataPoint : IDataPoint
{
public TwoTuple<float> Coords { get; }
public float Value { get; }
public BarDataPoint(TwoTuple<float> Coords, float Value)
{
this.Coords = Coords;
this.Value = Value;
}
public override string ToString()
{
return "Coordinates: " + Coords + "; Value: " + Value;
}
}
public struct ScatterDataPoint : IDataPoint
{
public ThreeTuple<float> Coords { get; }
public float Value { get; }
public ScatterDataPoint(ThreeTuple<float> coords, float value)
{
this.Coords = coords;
this.Value = value;
}
public override string ToString()
{
return "Coordinates: " + Coords + "; Value: " + Value;
}
}
界面
public interface IDataPoint<TTuple>
{
float Value { get; }
TTuple Coords { get; }
string ToString();
}
用法
public struct BarDataPoint : IDataPoint<ThreeTuple<float>>
{
public float Value { get; }
public ThreeTuple<float> Coords { get; set; }
public BarDataPoint(ThreeTuple<float> Coords, float Value)
{
this.Coords = Coords;
this.Value = Value;
}
public override string ToString()
{
return "Coordinates: " + Coords + "; Value: " + Value;
}
}
通過在接口聲明中提供泛型及其約束,像在ITuple
接口中那樣做:
public interface IDataPoint<T,T2> where T : ITuple<T2>
{
float Value { get; }
T Coords { get; }
string ToString();
}
您的Coords
屬性現在具有類型T
,並且T
必須實現接口ITuple<T2>
。 你的類BarDataPoint
應該像這樣定義:
public struct BarDataPoint : IDataPoint<TwoTuple<float>,float>
您必須將ITuple<T>
的泛型類型告訴編譯器兩次。 在線演示: https : //dotnetfiddle.net/CYw1bR
你可以做的是:
public interface IDataPoint
{
float Value { get; }
ITuple Coords { get; }
string ToString();
}
你不需要泛型。 ITuple
是元組的通用基礎實現。 這些都是從ITuple
派生出來的
System.Tuple<T1>
System.Tuple<T1,T2>
System.Tuple<T1,T2,T3>
System.Tuple<T1,T2,T3,T4>
System.Tuple<T1,T2,T3,T4,T5>
System.Tuple<T1,T2,T3,T4,T5,T6>
System.Tuple<T1,T2,T3,T4,T5,T6,T7>
System.Tuple<T1,T2,T3,T4,T5,T6,T7,TRest>
System.ValueTuple
System.ValueTuple<T1>
System.ValueTuple<T1,T2>
System.ValueTuple<T1,T2,T3>
System.ValueTuple<T1,T2,T3,T4>
System.ValueTuple<T1,T2,T3,T4,T5>
System.ValueTuple<T1,T2,T3,T4,T5,T6>
System.ValueTuple<T1,T2,T3,T4,T5,T6,T7>
System.ValueTuple<T1,T2,T3,T4,T5,T6,T7,TRest>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.