是否可以调用base.base函数? [英] Is it possible to call a base.base function?
本文介绍了是否可以调用base.base函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有
class A
{
受保护 虚拟 void OnActivate ()
{
...
}
...
}
class B:
{
受保护 覆盖 void OnActivate()
{
base .OnActivate();
...
}
...
}
class C:B
{
受保护 覆盖 void OnActivate()
{
????
}
...
}
我的问题是,C类是否有可能在不调用B类方法的情况下调用A类的OnActivate?如果是这样,怎么样?
谢谢
布伦特
我尝试了什么:
在C类复制A类代码。
解决方案
不,你想要的方式是不可能的,这是有充分理由的:你会违反其中一项OOP原则:多态(计算机科学) - 维基百科 [ ^ ]
来自Eric Lippert的StackOverflow完全相同的问题的一个很好的答案,他是C#语言设计师之一: c# - 如何调用base.base.method()? - Stack Overflow [ ^ ]
PS:如果您认为自己需要这种设计,那么您的课堂设计是有缺陷的,您应该重做它而不是试图以某种方式混淆它。
I have
class A
{
protected virtual void OnActivate()
{
...
}
...
}
class B : A
{
protected override void OnActivate()
{
base.OnActivate();
...
}
...
}
class C : B
{
protected override void OnActivate()
{
????
}
...
}
My question is, is it possible for class C to call class A's OnActivate without calling class B's method? If so, how?
Thanks
Brent
What I have tried:
Duplicating class A's code in class C.
解决方案
No, it's not possible in the way you want it and that's for a good reason: You would violate one of the OOP-principles: Polymorphism (computer science) - Wikipedia[^]
A great answer to exactly the same question on StackOverflow from Eric Lippert, one of the C# language designers: c# - How to call base.base.method()? - Stack Overflow[^]
PS: If you think you need this your class design is flawed and you should rework it instead of trying to muddle through it somehow.
这篇关于是否可以调用base.base函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文