1

私は疑問に思っていました:interface別のクラスから継承できますか?

からインターフェイスを継承させようとしていますMarshalByRefObject

私の意図は、インターフェイスを実装するすべてのクラスもそのクラスから継承することです。

4

5 に答える 5

7

No, it cannot.
An interface can only specify other interfaces that must be implemented. This is done by using the same syntax as inheritance, but it's something different.

You could use an abstract class instead that inherits from MarshalByRefObject and and requires your interfaces to be implemented.

Depending on how you need to enforce your requirement, generic constraints might help, too. For generic type parameters, you can set class constraints, like class Argh<T> where T : MarshalByRefObject, ISomeInterface.

于 2012-05-23T08:41:21.450 に答える
3

インターフェイスはクラスから継承できません。そのためには、C# は現在サポートされていない実装の多重継承をサポートする必要があります。

IMyInterfaceクラス からインターフェイス を派生できると想像してくださいMyClass。次に、そのインターフェイスを実装する別のクラスを宣言するときは、次のように記述する必要があります。

public class MyImplementingClass: MyBaseClass, IMyInterface

MyBaseClassただし、との両方から継承しているため、これは実装の多重継承を意味しますMyClass

于 2012-05-23T08:42:15.697 に答える
2

いいえ。クラスはインターフェイスを実装できます。その逆ではありません。

  1. クラスはクラスを継承し、インターフェースを実装できます。
  2. インターフェイスはインターフェイスのみを実装できますが、クラスを継承したり実装したりすることはできません。
于 2012-05-23T08:49:57.207 に答える
2

いいえ、インターフェイスには実装を含めることができないため、クラスから継承することはできません。ただし、から継承する抽象クラスを作成できますMarshalByRefObject

于 2012-05-23T08:42:46.483 に答える
2

No, but an interface can inherit from another interface.

于 2012-05-23T08:41:26.150 に答える