/*???*/
(それ以外の場合はfirstclass
)には、継承するすべてのクラスでオーバーライドすることなく、呼び出すクラスの型を返すことができますこのメソッドには、すべての用途に接頭辞なしで(secondclass)
が付いています。基本的にはthis.GetType()
の意味ですか?継承したクラスの戻り値の型を持つ継承されたメソッド
public class firstclass
{
private static /*???*/ current;
public static /*???*/ modifycurrent()
{
/*do stuff to current*/
return current;
}
}
public class secondclass : firstclass
{
//nothing concerning the above
}
//usage
secondclass a = new secondclass();
secondclass b = a.modifycurrent(); //<--no cast here
目標は、タイプsecondclass
の変数を共有するsecondclass
のすべてのインスタンスに対してです。そして、私がfirstclass
から継承している3番目、4番目、5番目のクラスを作るときには、戻り値のタイプがmodifycurrent()
であること以外は、firstclass
の正確なミラーであるこれらのメソッドをすべて書き直す必要はありません。この例では、以前に開かれたインスタンスを追跡するすべてのタイプのフォームがあります。静的メンバが継承されていないので、私はstatic
を脱い
public class firstclass<T> where T:firstclass
{
private T current;
public T modifycurrent()
{
/*do stuff to current*/
return current;
}
}
public class secondclass : firstclass<secondclass>
{
//nothing concerning the above
}
注:あなたは不思議経常テンプレートのパターンを使用するように
modifycurrentは静的なので、クラスインスタンスを介して呼び出すことはできません...とにかくあなたが求めるものは奇妙です、あなたは何を達成したいのですか? –
aとbは2番目のクラスですか?キャスティングは全くありません。私はあなたが達成しようとしていることを理解できません。おそらくbはfirstclassですか? – Gusman
この種の問題を解決しようとするときは、元の問題を解決する代わりに、コードを別の方法で書き直してください。 –