C# 可以用另一个基类重写基类中的一个抽象方法吗?
我有一个基类,上面有一些抽象方法,有21个类继承自这个基类。现在对于其中一个抽象方法,我想用21个类中的6个类的通用实现来实现它,所以我考虑创建另一个基类来实现这一点 我愿意接受建议,但我在当前基类和21个类之间创建另一个基类的主要目的是避免在21个类中的6个类中重复相同的代码 下面是一个示例代码来说明这种情况:C# 可以用另一个基类重写基类中的一个抽象方法吗?,c#,inheritance,abstract-class,C#,Inheritance,Abstract Class,我有一个基类,上面有一些抽象方法,有21个类继承自这个基类。现在对于其中一个抽象方法,我想用21个类中的6个类的通用实现来实现它,所以我考虑创建另一个基类来实现这一点 我愿意接受建议,但我在当前基类和21个类之间创建另一个基类的主要目的是避免在21个类中的6个类中重复相同的代码 下面是一个示例代码来说明这种情况: public abstract class FooBase { public abstract string Bar(); public abstract string S
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";
}
}
是的,这是完全可能的,而且经常发生。没有规则规定在类层次结构的最底层只能有一个基类;该类的子类也可以是抽象的,从而成为间接派生自通用基类的一组类的基类(稍微专业化一些)。您应该指定这些类的确切功能,但是。。根据您提供的信息: 这正是本文要解决的问题,如Head First Design Patterns一书中给出的示例所示 您有一个抽象的
Duck
类,其他Duck(例如,RedheadDuck
,MallardDuck
)从中派生。Duck
类有一个Quack
方法,它只在屏幕上显示字符串“Quack”
现在,您被告知添加一个RubberDuck
。这家伙不会呱呱叫!那你是做什么的?让Quack
抽象化,让子类决定如何实现它?不,这会导致代码重复
相反,您可以使用Quack
方法定义iQuickBehavior
接口。从那里,您派生出两个类,quackbehavior
和SqueakBehaviour
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();
}
}
如果希望鸭子在运行时更改其行为,您甚至可以通过属性插入
iQuickBehavior
的实例。是的,您可以从另一个抽象类派生一个抽象类
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
有一个通用的定义。你的想法似乎是对的。你试过了吗?你遇到什么问题了吗?谢谢,这很有效!