0

私はこのインターフェースを持っています

public interface TestInterface
{
   [returntype] MethodHere();
}

public class test1 : TestInterface
{
   string MethodHere(){
      return "Bla";
   }
}

public class test2 : TestInterface
{
   int MethodHere(){
     return 2;
   }
}

[returntype]を動的にする方法はありますか?

4

2 に答える 2

5

戻り型をObjectとして宣言するか、汎用インターフェースを使用します。

public interface TestInterface<T> {
    T MethodHere();
}

public class test3 : TestInterface<int> {
   int MethodHere() {
      return 2;
   }
}
于 2010-01-16T22:15:20.143 に答える
4

それほど動的ではありませんが、一般的にすることができます。

public interface TestInterface<T>
{
    T MethodHere();
}

public class Test1 : TestInterface<string>
... // body as before
public class Test2 : TestInterface<int>
... // body as before

それが目的ではない場合は、インターフェースの使用方法について詳しく教えてください。

于 2010-01-16T22:14:39.997 に答える