简体   繁体   English

我可以从基类中调用孩子的父方法吗?

[英]Can I call the child's parent method from the base class?

How can i do this: 我怎样才能做到这一点:

class BaseClass{
  void templateMethod(){
     methodImplementation();
     if(this.base.getType() != typeof(BaseClass)) this.base.methodImplementation();
  }
  void abstract methodImplementation();
}
class Derived1 : BaseClass{
  void override methodImplementation(){
     .. do things ..
  }
}
class Derived2 : Derived1{
  void override methodImplementation(){
     .. do more things ..
  }
}

This would then result in both implementations (things AND more things) being executed. 然后,这将导致两个实现(事物以及更多事物)都被执行。

Derived2 object = new Derived2();
object.templateMethod();

Then all children and grand children and so on, would only need to implement their part of methodImplementation and it would get called throughout all levels when called in the final object. 然后,所有子代和大子代等等,仅需要实现他们的methodImplementation部分,并且在最终对象中调用时,它将在所有级别中调用。

But this.base is not a thing. 但是this.base并不是问题。 What I need is to call the implementation of the parent of the instantiated object real class, but the code is in the baseClass so I can use it in a Template method pattern. 我需要的是调用实例化对象真实类的父类的实现,但是代码在baseClass中,因此可以在Template方法模式中使用它。

Thanks in advance, sorry if I'm not making myself clear. 在此先感谢您,如果我不清楚自己的意思,对不起。 I just need to know if it can be done. 我只想知道是否可以做到。 This being good practice, optimal, recommended or useful is another thing that I'll consider. 我会考虑的另一件事是,良好的做法,最佳的建议或有用的做法。

EDIT: 编辑:

The code above is not exactly right, it should be like this: 上面的代码并不完全正确,应该是这样的:

class BaseClass{
  void templateMethod(){
     methodImplementation();
     if(this.base.getType() != typeof(BaseClass)) this.base.templateMethod();
  }
  void abstract methodImplementation();
}
class Derived1 : BaseClass{
  void override methodImplementation(){
     .. do things ..
  }
}
class Derived2 : Derived1{
  void override methodImplementation(){
     .. do more things ..
  }
}

the difference being that in BaseClass.templateMethod() what gets called in the second line is templateMethod(), not methodImplementation(), so it calls itself but from the "perspective" of the parent class. 区别在于,在BaseClass.templateMethod()中,第二行调用的是templateMethod(),而不是methodImplementation(),因此它是从父类的“视角”调用自身的。 Now I'm thinking more and more this is just not possible and creates more confusion than anything. 现在,我越来越想这是不可能的,并且比任何事情都引起更多的混乱。

If I understand you correctly, you want to call all methodImplementation from top level derived class. 如果我对您的理解正确,则希望从顶级派生类调用所有methodImplementation You can do it like this: 您可以这样做:

class BaseClass{
  void templateMethod(){
     methodImplementation();
  }
  void abstract methodImplementation();
}
class Derived1 : BaseClass{
  // virtual as you want to derive it again
  virtual void override methodImplementation(){
     //.. do things ..
  }
}
class Derived2 : Derived1{
  void override methodImplementation(){
     //.. do more things ..
     base.methodImplementation();
  }
}

Each derived class only has to implement his part of methodImplementation and at the end call their base class. 每个派生类仅需实现methodImplementation一部分,最后调用其基类。 Since methodImplementation is abstract dont call base on first level. 由于methodImplementationabstract不要呼叫base的第一级。 Also mark all methodImplementation as virtual so you can override it again. methodImplementation所有methodImplementation标记为虚拟,以便您可以再次覆盖它。

You can call the base method in your derived classes. 您可以在派生类中调用基本方法。 Here's an example. 这是一个例子。 You can call the base method in each line of your derived class; 您可以在派生类的每一行中调用基本方法。 depending on when you want to execute the logic. 取决于您何时要执行逻辑。

public class Animal
{
    public void WhatAmI()
    {
        Console.WriteLine("Animal");
    }
}

public class Fish : Animal
{
    public new void WhatAmI()
    {
        base.WhatAmI();
        Console.WriteLine("Fish");
    }
}

public class HammerheadShark : Fish
{
    public new void WhatAmI()
    {
        base.WhatAmI();
        Console.WriteLine("HammerheadShark");
    }
}

When you call the WhatAmI method: 当您调用WhatAmI方法时:

var animal = new HammerheadShark();
animal.WhatAmI();

You'll get this: 您会得到:

Animal 动物

Fish

HammerheadShark 锤头鲨

好的,即使有一个复杂的方法可以做到这一点,它也根本无法满足我的需要,我想它对任何形式的体面代码都没有用。

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

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