在 C# 的不同 类 中使用方法子集作为成员方法

Use a subset of methods as member methods in different classes in C#

是否可以在 C# 中的不同 classes 中使用方法子集作为成员方法?

例如,我有四个函数 void A()void B()void C()void D()

现在我想要三个 classes。第一个 class 我想要成员方法 ABC。第二个我想要 BD。第三个 ACD.

我怎样才能做到这一点?是否可以仅通过使用接口来实现此目的,或者是否有任何其他方法?

如果您的三个 类 都应该对这些方法有 不同的实现 ,那么经典接口就是最佳选择:

public interface IHaveA {
    void A();
}

public interface IHaveB {
    void B();
}

public interface IHaveC {
    void C();
}

public interface IHaveD {
    void D();
}

public class Class1 : IHaveA, IHaveB, IHaveC { // Implement A, B and C here }

public class Class2 : IHaveB, IHaveD { // Implement B and D here  }

public class Class3 : IHaveA, IHaveC, IHaveD { // Implement A, C and D here  }

如果您的所有 类 都应该具有 ABCD[=30] 相同的实现=],你仍然可以使用接口,但你必须复制代码:

public static class StaticImplementation {
    public void A(IHaveA sender) {
        // Do stuff here
    }

    public void B(IHaveB sender) {
        // Do stuff here
    }
    public void C(IHaveC sender) {
        // Do stuff here
    }

    public void D(IHaveD sender) {
        // Do stuff here
    }
}

public class Class1 : IHaveA, IHaveB, IHaveC {
    public void A() { StaticImplementation.A(this) }
    public void B() { StaticImplementation.B(this) }
    public void C() { StaticImplementation.C(this) }
}

public class Class2 : IHaveB, IHaveD { // Calls to StaticImplementation for B and D here  }

public class Class3 : IHaveA, IHaveC, IHaveD { // Calls to StaticImplementation for A, C and D here  }

无法强制这三个 类 使用接口对这些方法具有相同的实现,因为接口的主要目标是确保 类 实现方法,具体而言具有不同的实现!

这在 C# 8.0 和 .NET Core 3.0 中发生了变化,您可以在其中使用接口方法的默认实现,并且可以通过密封它们来确保这些实现不会改变。

代码将变为:

public interface IHaveA {
    sealed void A() {
        // Implementation here
    }
}

public interface IHaveB {
    sealed void B() {
        // Implementation here
    }
}

public interface IHaveC {
    sealed void C() {
        // Implementation here
    }
}

public interface IHaveD {
    sealed void D() {
        // Implementation here
    }
}

public class Class1 : IHaveA, IHaveB, IHaveC { // Nothing to do here }

public class Class2 : IHaveB, IHaveD { // Nothing to do here  }

public class Class3 : IHaveA, IHaveC, IHaveD { // Nothing to do here  }
using System;

public interface MA {}
public static class MAProvider {
    public static void A(this MA obj) { Console.WriteLine("MA"); }
}

public interface MB {}
public static class MBProvider {
    public static void B(this MB obj) { Console.WriteLine("MB"); }
}

public interface MC {}
public static class MCProvider {
    public static void C(this MC obj) { Console.WriteLine("MC"); }
}

public interface MD {}
public static class MDProvider {
    public static void D(this MD obj) { Console.WriteLine("MD"); }
}

public class First : MA, MB, MC {}
public class Second : MB, MD {}
public class Third : MA, MC, MD {}

public static class Program {
    public static void Main() {
        new First().A();
        new First().B();
        new First().C();

        new Second().B();
        new Second().D();

        new Third().A();
        new Third().C();
        new Third().D();
    }
}

剩下的唯一问题是 private 成员。由于我们无法在扩展方法中访问它们。