呼び出すことができるスーパークラスclass Aといくつかのサブクラスがclass a1 : Aありclass a2 : Aますa6 : A。myには、サブクラスの 1 つを作成してin にclass B追加する一連のメソッドがあります。List<A>B
現在持っているコードを短くしたい。だから書く代わりに
Adda1()
{
aList.Add( new a1() );
}
Adda2()
{
aList.Add( new a2() );
}
...
Adda6()
{
aList.Add( new a6() );
}
代わりに、これに似たものを書きたい
Add<T>()
{
aList.Add( new T() ); // This gives an error saying there is no class T.
}
それは可能ですか?
TタイプAまたはそのサブクラスの1つである必要がある制約も可能ですか?