如果派生类有同名的方法,如何从派生类调用基类方法? [英] how do I call a base class method from the derived class, if the derived class has a method with the same name?
问题描述
这是我想要能够做的:(伪代码)
this is what I want to be able to do: (pseudo code)
class DerivedClass : public BaseClass
{
public Draw()
{
BaseClass.Draw()
}
}
class BaseClass
{
protected Draw();
}
两个绘图都有相同的名称和相同的签名。想要这样做的原因是,有时我想要我的派生类有一个绘制函数,只是调用基类绘制,但在其他时候,我想让派生类选择什么时候调用基本绘制函数或不。这意味着我可以保持我实例化派生类更清洁的类,并可以在所有时间调用它们的所有。派生类本身处理nitty-gritty。
Both draws have the same name and the same signature. The reason for wanting to do this is that sometimes I want my derived classes to have a draw function that simply calls the Base Classes draw, but at other times I want the derived class to choose when to call the base draw function or not. This meant that I can keep the class that I instantiate the derived classes in much cleaner, and can just call draw on all of them at all times. The derived classes themselves handle the nitty-gritty.
BaseClass.Draw
部分的语法究竟是什么?我想你可能只是写的,因为它是,但编译器抱怨,它不像我可以调用 Draw
,因为签名是相同的。
What exactly is the syntax for the BaseClass.Draw
part? I thought you could actually just write that as it is, but the compiler is complaining, and it's not like I can just call Draw
because the signatures are the same.
推荐答案
语法是 BaseClass :: Draw()
。
这篇关于如果派生类有同名的方法,如何从派生类调用基类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!