的特質對不起,如果這是措辭不佳,或者如果這之前已經問過,但我似乎無法找到任何與此有關的事情,我很累。獲取子類
好的,所以我想要做的是獲得我的特質在子類中的值的情況下,我需要引用一個子類的實例,但我沒有關於它會用什麼特性的信息正在使用。這對我來說更容易在代碼中解釋,所以這就是我正在嘗試做的事情。
public class TraitUser<T>
{
public void DoThingWithT(T thing)
{
thing.ToString();
}
}
public class TraitInspector
{
public void DoThing()
{
// This is where I run into my issue,
// I need to be able to get the trait that
// an instance of the TraitUser class is using to continue.
TraitUser<> tUser = GetRandomTraitUser()/*Imagine this returns an instance of TraitUser with a random trait, this is where my issue comes in.*/;
}
}
編輯:修正了我的一些皮膚問題,現在應該會更容易理解。
你寫了兩個不同的類。哪個是子類? – LPs
我編輯過你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –