C#:有什么方法可以跳过多态性中的一个基调用吗?
class GrandParent
{
public virtual void Foo() { ... }
}
class Parent : GrandParent
{
public override void Foo()
{
base.Foo();
//Do additional work
}
}
class Child : Parent
{
public override void Foo()
{
//How to skip Parent.Foo and just get to the GrandParent.Foo base?
//Do additional work
}
}
如上面的代码所示,如何让 Child.Foo() 调用 GrandParent.Foo() 而不是调用 Parent.Foo()? base.Foo()
首先将我带到 Parent 类。
class GrandParent
{
public virtual void Foo() { ... }
}
class Parent : GrandParent
{
public override void Foo()
{
base.Foo();
//Do additional work
}
}
class Child : Parent
{
public override void Foo()
{
//How to skip Parent.Foo and just get to the GrandParent.Foo base?
//Do additional work
}
}
As the code above shows, how can I have the Child.Foo() make a call into GrandParent.Foo() instead of going into Parent.Foo()? base.Foo()
takes me to the Parent class first.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果你需要这个,你的设计就是错误的。
相反,请将每个类的逻辑放在
DoFoo
中,并且在不需要时不要调用base.DoFoo
。Your design is wrong if you need this.
Instead, put the per-class logic in
DoFoo
and don't callbase.DoFoo
when you don't need to.我认为你的设计有问题。本质上,你想要“打破”多态性的规则。您说
Child
应该从Parent
派生,但希望方便地跳过其父级中的实现。重新思考你的设计。
I think there is something wrong with your design here. Essentially, you want to "break" the rules of polymorphism. You are saying
Child
should derive fromParent
but want to conveniently skip the implementation in it's parent.Re-think your design.
不,无论如何这都不可靠。作为您的类的实现者,您可以选择您的直接基类。但谁说
Parent
的更高版本可能不会从ParentBase
继承,而后者又从GrandParent
继承呢?只要Parent
仍然实现正确的契约,这就不会对那些继承自Parent
的类造成任何问题。No. It wouldn't be reliable anyway. You, as the implementer of your class, get to choose your immediate base class. But who is to say that a later release of
Parent
might not inherit fromParentBase
, that in turn inherits fromGrandParent
? So long asParent
is still implementing the correct contract, this should not cause any issues for those classes inheriting fromParent
.不,这是不可能的。想象一下,如果这可能的话,事情将会多么疯狂。
如果您希望在
Child
情况下跳过某些特定内容,请考虑重新设计您的设计以更好地表示您需要的内容(例如,也许您还需要覆盖Child
类中的其他内容)。或者,您可以在Parent
类中提供另一个Foo()
,该类除了调用其base.Foo()
之外不执行任何操作。No, this isn't possible. Imagine how crazy things would be if this was possible.
If you want something specific skipped in the
Child
case, consider reworking your design to better represent what you need (e.g. maybe you need to override something else in theChild
class, too). Or, you could provide anotherFoo()
in theParent
class that doesn't do anything except call itsbase.Foo()
.如果您可以控制代码,最简单的方法是在父类中创建一个仅调用 base.Foo() 的受保护方法,并且您的子类 Foo 实现显式调用该方法
If you have control of the code, the simplest way is to create a protected method in Parent class that only call base.Foo() and your child class Foo implementation call that method explicitly
我们在一个大型项目中遇到过这种情况,其中派生方法是从不同位置调用的。由于变更管理和 QA 脚本不能被破坏,以及其他限制,在大型成熟项目中并不总是可能进行“剧烈”重构和类重构。此外,我们不想覆盖该方法并排除所有基本功能。在其他地方看到的大多数解决方案看起来有点笨拙,但是 Josh Jordan 关于如何调用的解决方案base.base 非常有用。
然而,我们遵循了下面的方法(我现在看到的方法与丹·阿布拉莫夫的提议非常相似)。
We had exactly this scenario on a large project where the derived methods were called from various locations. Due to change management and QA scripts not to be broken, among other constraints, "drastic" refactoring and class re-structuring are not always possible on a large mature project. Also we did not want to override the method and exclude all base functionality. Most solutions seen elsewhere, looked a bit clumsy, but the solution from Josh Jordan on How to call base.base was quite useful.
However we followed the approach below (which I see now is very similar to what Dan Abramov propose).
所有这些强烈的意见...
有时使用 99% 的东西才有意义...
All these strong opinions...
Sometimes it just makes sense to use 99% of something...