可以用另一个基类重写基类中的一个抽象方法吗?

本文关键字:基类 一个 抽象方法 另一个 重写 | 更新日期: 2023-09-27 17:49:39

我有一个基类,它有一些抽象方法,有21个类是从这个基类继承的。现在,对于其中一个抽象方法,我想用21个类中的6个类的通用实现来实现它,所以我考虑创建另一个基类来实现它。

我愿意接受建议,但我在当前基类和21个类之间创建另一个基类的主要目的是避免在21个类中的6个类中重复相同的代码,如果我不需要的话。

下面是一个示例代码来说明这种情况:

public abstract class FooBase
{
   public abstract string Bar();
   public abstract string SomeMethod();
   public virtual string OtherMethod()
   {
       return this.SomeMethod();
   }
}
public abstract class AnotherBase : FooBase
{
   public abstract string Bar();
   public abstract string SomeMethod();
   public override OtherMethod()
   {
      //this is the common method used by 6 of the classes
      return "special string for the 6 classes";
   }
}
public class Foo1 : FooBase
{
   public override string Bar()
   {
      //do something specific for the Foo1 class here
      return "Foo1 special string";
   }
   public override string SomeMethod()
   {
      //do something specific for the Foo1 class here
      return "Foo1 special string";
   }
}
public class Another2 : AnotherBase
{
   public override string Bar()
   {
      //do something specific for the Another2 class here
      return "Another special string";
   }
   public override string SomeMethod()
   {
      //do something specific for the Another2 class here
      return "Another2 special string";
   }
}

可以用另一个基类重写基类中的一个抽象方法吗?

是的,你可以从另一个抽象类派生出一个抽象类

public abstract class FooBase
{
    //Base class content
}
public abstract class AnotherBase : FooBase
{
    //it is "optional" to make the definition of the abstract methods of the Parent class in here
}

当我们说可选在子类内部定义父类的抽象方法时,是强制要求子类必须是抽象的

public abstract class FooBase
{
    public abstract string Bar();
    public abstract string SomeMethod();
    public abstract string OtherMethod();
}
public abstract class AnotherBase : FooBase
{
    public override string OtherMethod()
    {
        //common method that you wanted to use for 6 of your classes
        return "special string for the 6 classes";
    }
}
//child class that inherits FooBase where none of the method is defined
public class Foo1 : FooBase
{
    public override string Bar()
    {
        //definition
    }
    public override string SomeMethod()
    {
        //definition
    }
    public override string OtherMethod()
    {
        //definition
    }
}
//child class that inherits AnotheBase that defines OtherMethod
public class Another2 : AnotherBase
{
    public override string Bar()
    {
        //definition
    }
    public override string SomeMethod()
    {
        //definition
    }
}

所以我猜还会有5个像Another2这样的继承自AnotherBase的类,它们会对OtherMethod有一个共同的定义

是的,这是完全可能的,而且经常这样做。没有规则说你只能在类层次结构的最底层有一个基类;该类的子类也可以是抽象的,从而成为间接派生自通用基类的一组类的基类。

您应该指定这些类究竟做什么,但是…根据您提供的信息:

这正是策略模式要解决的问题,正如Head First Design Patterns书中给出的例子所示。

您有一个抽象的Duck类,其他鸭子(例如,RedheadDuck, MallardDuck)从中派生。Duck类有一个Quack方法,它只是在屏幕上显示字符串"庸医"。

现在你被告知添加一个RubberDuck。这家伙不嘎嘎叫!那么你会怎么做呢?使Quack抽象,让子类决定如何实现这一点?不行,那样会导致代码重复。

相反,您可以使用Quack方法定义IQuackBehaviour接口。从那里,您派生了两个类,QuackBehaviourSqueakBehaviour
public class SqueakBehaviour: IQuackBehaviour
{
    public void Quack(){
        Console.WriteLine("squeak");
    }
}
public class QuackBehaviour: IQuackBehaviour
{
    public void Quack(){
        Console.WriteLine("quack");
    }
}

现在,你用合适的行为组成你的鸭子:

public class MallardDuck : Duck
{
    private IQuackBehaviour quackBehaviour = new QuackBehaviour();
    public override void Quack()
    {
        quackBehaviour.Quack();
    }
}
public class RubberDuck : Duck
{
    private IQuackBehaviour quackBehaviour = new SqueakBehaviour();
    public override void Quack()
    {
        quackBehaviour.Quack();
    }
}

如果你希望鸭子在运行时改变它们的行为,你甚至可以通过属性注入IQuackBehaviour的实例。