[英]Inheritance of nested generics / nested generics in abstract class C#
我有兩個抽象類可以被顯式使用:A_GUI_Info 和 A_Info_Data。 GUI_Info 是顯示數據的 GUI 元素。 Info_Datas 是數據類,將特定數據傳輸到相應的 GUI_Info。
我想通過泛型表達顯式 GUI_Info 具有一個顯式 Info_Data 並且仍然允許繼承的依賴性。 換句話說,我想避免將錯誤的顯式 Info_Data 饋送到顯式 GUI_Info。 例如,我將 HUD_Info_Data 提供給無法表示它的 Wrist_GUI_Element。 > 一種繼承泛型的類型安全
例子:
class HUDInfoData : A_Info_Data
class HUDInfo<HUDInfoData > : A_GUI_Info<A_Info_Data>
// but the generic cant be inherited like that
class HUDInfo : A_GUI_Info<A_Info_Data>
// doesnt define dependency
class HUDInfo : A_GUI_Info<HUDInfoData >
// also not working
另一種方法是限制where T : A_GUI_Info<D> where D : A_Info_Data
但它並沒有像那樣工作。
我無法開始工作的最后一個要求是:我有一個顯式信息的實例,並希望在一個函數中處理它,該函數還可以處理所有其他繼承的信息及其相應的數據。
public HUD_Info<HUD_Info_Data> obj;
public List<A_GUI_Info<A_Info_Data>> infos;
public void SetConnection(string ID, A_GUI_Info<A_Info_Data> p)
{
infos.Add(p);
}
最終可能需要使用這種數據結構:
public abstract class A_GUI_Info<G, D>
where G : A_Info_Data<G, D>
where D : A_GUI_Info<G, D>
{
public G Gui { get; set; }
}
public abstract class A_Info_Data<G, D>
where G : A_Info_Data<G, D>
where D : A_GUI_Info<G, D>
{
public D Data { get; set; }
}
這不是太好,但它確實將兩個派生類型相互聯系起來。
你可以這樣定義它們:
public class HUDInfoData : A_Info_Data<HUDInfoData, HUDInfo>
{
}
public class HUDInfo : A_GUI_Info<HUDInfoData, HUDInfo>
{
}
你有沒有嘗試過:
abstract class A_Info_Data { ... }
abstract class A_GUI_Info<T> where T: A_Info_Data { ... }
現在:
class CriticalData: A_Info_Data { ... }
class CriticalGui: A_GUI_Info<CriticalData> { ... }
基類上的類型參數只存在於基類上。 派生程度更高的類必須定義一個新的類型參數,並通過管道將類型傳遞到基類的類型參數。 這為您提供了一個提出更通用約束的地方。
例如:
class HUDInfo<THudInfoData> : A_GUI_Info<THudInfoData> where THudInfoData : A_Info_Data
現在, HUDInfo<>
可以采用任何HudInfoData
,只要它源自A_Info_Data
(或者如果它是A_Info_Data
)。 如果你想有HUDExtremelySpecificInfo
這只能采取HUDExtremelySpecificInfoData
,這將是這樣的:
class HUDExtremelySpecificInfo<THudInfoData> : A_GUI_Info<THudInfoData>
where THudInfoData : HUDExtremelySpecificInfoData
如果您永遠不想指定類型,因為您知道它總是HUDExtremelySpecificInfoData
,您也可以同時聲明:
class HUDExtremelySpecificInfo<THudInfoData> : A_GUI_Info<THudInfoData>
where THudInfoData : HUDExtremelySpecificInfoData { .. }
class HUDExtremelySpecificInfo : HUDExtremelySpecificInfo<HUDExtremelySpecificInfoData> { .. }
(您根據通用HUDExtremelySpecificInfo<>
實現非通用HUDExtremelySpecificInfo
,如果您想要指定一個特定的更具體的信息數據子類,則可以使用通用的)
或者只是一個:
class HUDExtremelySpecificInfo : A_GUI_Info<HUDExtremelySpecificInfoData> { .. }
謝謝大家給出建設性的答案。 我在這里嘗試的方法並不像首選(pbbly 根本不可能)。 這就是我想出的:
// abstract definitions
public abstract class AInfo
{
public abstract void SetData(AInfoData data);
}
public abstract class AInfoData
// explicit definitions
public class WristInfo : AInfo
{
public override void SetData(AInfoData data)
{
Data_Info_Wrist d = (Data_Info_Wrist)data; // cast to use
}
}
public class Data_Info_Wrist : AInfoData
// runtime usage
public AInfo instance; (WristInfo)
public void Setup(AInfo info) { }
對於 Unity 工作流,我要求顯式類是非泛型的。 所以這個解決方法可能是最好的解決方案。 缺點:這里沒有給出所需的類型安全。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.