简体   繁体   English

具有基类函数取决于其子类C#

[英]Having a base class function depend on its child class C#

I have a Base class with a method that a child class will almost always override. 我有一个基类,它的方法几乎总是覆盖子类。 However, instead of replacing the base class' method entirely, I would like for whatever is derived in the child class to be added to what is already in the base class. 但是,我不想完全替换基类的方法,而是希望将子类中派生的任何内容添加到基类中已经存在的内容中。

For Example: 例如:

class BaseClass{

public string str()
 {
   return "Hello my name is" ;
 }
}

class ChildClass : BaseClass{

public override string str() 
 {
   return "Sam";
 }
}

The point is that if I want to access the str() method by creating an instance of the ChildClass, the string will print out as "Hello, my name is Sam". 关键是,如果我想通过创建ChildClass的实例来访问str()方法,则该字符串将打印为“ Hello,我的名字是Sam”。

I've been looking around and all I have been finding is that this should NOT happen, as the base class shouldn't even know that it is being inherited. 我一直在环顾四周,发现的一切都不应该发生,因为基类甚至都不知道它正在被继承。 So, if the design is false, how would I go about doing this? 那么,如果设计错误,我将如何进行? Keep in mind that there will be multiple child classes inheriting from BaseClass. 请记住,会有多个子类从BaseClass继承。

Thank you 谢谢

If you always want to do this, and you don't want to rely on implementers of the derived classes to remember to override the method and call into the base class, you can use a template pattern: 如果您总是想这样做,并且不想依赖派生类的实现者来记住重写方法调用基类,则可以使用模板模式:

public abstract class BaseClass
{
    public string str()
    {
        return "Hello my name is " + Name;
    }

    protected abstract string Name { get; }
}

Now any non-abstract class that inherits BaseClass will be required by the compiler to override Name , and that value can be consumed by BaseClass . 现在,编译器将需要任何继承BaseClass非抽象类来覆盖Name ,并且BaseClass可以使用该值。

public class ChildClass : BaseClass
{
    protected override string Name
    {
        get { return "Sam"; }
    }
}

Obviously, this depends on the base class being abstract . 显然,这取决于基类是否为abstract

If you want to call base class method: 如果要调用基类方法:

1.Declare the method in base class as virtual , so that you can override from child class. 1.将基类中的方法声明为virtual ,以便可以从子类中override

2.Use base to call method from base class. 2.使用base从基类调用方法。

internal class BaseClass
{
    public virtual string str()
    {
        return "Hello my name is" ;
    }
}

class ChildClass : BaseClass
{
    public override string str(){
        return base.str() + "Sam";
    }
}

First you have to make your method virual otherwise you can't override it: 首先,您必须使方法virual否则无法覆盖它:

public class BaseClass
{
  public virual string Str()
  {
    return "Hello my name is ";
  }
}

And then in your child class you can use base to access the method in your base class. 然后,在子类中,可以使用base访问基类中的方法。

public class ChildClass : BaseClass
{
  public override string Str()
  {
    return base.Str() + "Sam";
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM