C# - 接口/类设计问题

use*_*255 2 c# oop

我有一个A,B和C类通用的接口.但是现在我需要添加两个方法,它们只适用于B类,不适用于A类和C类.那么,我是否需要将这两个方法添加到公共接口本身并在A和C类中抛出未实现的异常,或者有更好的方法吗?

interface ICommon
{
   Method1;
   Method2;
   Method3;
   Method4;
}

Class A: ICommon
{
   Method1;
   Method2;
}

Class B: ICommon
{
   Method1;
   Method2;
   Method3;
   Method4;
}

Class C: ICommon
{
   Method1;
   Method2;
}
Run Code Online (Sandbox Code Playgroud)

提前致谢

Ste*_*ler 8

如果这些方法对于其他类(不仅仅是B)是通用的:

让B扩展另一个接口

interface ICommon2
{
    Method3;
    Method4;
}

class B : ICommon, ICommon2
{
    Method1;
    Method2;
    Method3;
    Method4;
}
Run Code Online (Sandbox Code Playgroud)

如果这些方法仅针对B:

class B : ICommon
{
    Method1;
    Method2;
    Method3;
    Method4;
}
Run Code Online (Sandbox Code Playgroud)

  • 根据具体情况,将"ICommon2"继承自"ICommon"也是一个好主意. (4认同)