0

这实际上是一个设计问题。
假设我有一个接口a和一个带有抽象方法m1m2的类aa,它继承自a

public interface a
{
  string m1();
  string m2();
}

public abstract class aa:a
{
  public abstract string m1();
  public abstract string m2();
}  

我还有一个不同的接口b和一个继承自它的类bb 。

public interface b
{
  string n1();
  string n2();
}

public abstract class bb:b
{
  public abstract string n1();
  public abstract string n2();
}  

我想强制继承aa的类实现bb类的方法(与实现m1m2相同)

正确的做法是什么?

public abstract class aa:a
{
  public abstract string m1();
  public abstract string m2();
  public abstract bb()?? 
  or public abstract bb.n1()???
}
4

1 回答 1

1

您可以执行以下操作之一:从 bb 类继承aa;在aa类中 实现b接口;在 aa 类中显式实现b 接口:

// Inherit aa from bb class
public abstract class aa:a, bb {
  public abstract string m1();
  public abstract string m2();

  // public abstract string n1(); // <- From bb class
  // public abstract string n2(); // <- From bb class
}

// Implement b interface in aa class
public abstract class aa:a, b {
  public abstract string m1();
  public abstract string m2();

  public abstract string n1(); // <- From b interface
  public abstract string n2(); // <- From b interface
}

// Implement b interface in aa class explicitly
public abstract class aa:a, b {
  public abstract string m1();
  public abstract string m2();

  protected abstract string core_n1(); // <- Put (override) b interface implementation here
  protected abstract string core_n2(); // <- Put (override) b interface implementation here

  // Can't be abstract itself but can call an abstract method
  b.string n1() { // <- explicit implementation, mo public or other modifier here!
    return core_n1();
  }
  // Can't be abstract itself but can call an abstract method
  b.string n2() { // <- explicit implementation, mo public or other modifier here!
    return core_n2();
  }
}
于 2013-09-02T08:14:27.607 回答