如何在界面中继承一些功能?

Sub*_*nda -1 c#

我有一个接口,其中包含假设的10个方法,它继承给类说class1.我的要求是我不想将2方法继承到class1.所以在c#.net中它是如何可能的

Pao*_*sco 6

您可以将界面拆分为2,并让您的类只实现其中一个:

public interface Ifc1 {
    void Method1();
}
public interface Ifc2 {
    void Method2();
}
class Cls1 : Ifc1 {
    // now you need to implement Method1 only
}
Run Code Online (Sandbox Code Playgroud)

或者,你可以抛出一个NotImplementedExceptionMethod2:

class Cls1 : Ifc1 {
    public void Method2() {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

如果可能的话,我会选择第一个选项.

如果抛出异常,则只会在调用时发现该方法不受支持.你将不得不捕获异常,你将编写的代码必须处理这个,并且不会那么干净.

如果你拆分接口,那么你的类就没有你想要实现的方法.