有没有办法指定范围?
考虑这个代码示例:
public abstract class Parent
{
public int val;
public Parent()
{
val = 0;
}
public virtual void foo()
{
inc();
}
public virtual void inc()
{
val = val + 10;
}
}
public class Child : Parent
{
public override void foo()
{
base.foo();
}
public override void inc()
{
val++;
}
}
static void Main(string[] args)
{
Parent p = new Child();
Console.WriteLine("p.val = " + p.val); //Output: p.val = 0
p.foo();
Console.WriteLine("P.val = " + p.val); //Output: p.val = 1
}
我假设父类的 inc()
没有被调用,因为 {this}
指针实际上指向子对象,因此子对象的版本inc()
将从父对象的函数 foo()
中调用。有没有办法强制父函数 foo()
始终调用父函数 inc()
就像在 C++ 中使用 ::
运算符一样?
Consider this code sample:
public abstract class Parent
{
public int val;
public Parent()
{
val = 0;
}
public virtual void foo()
{
inc();
}
public virtual void inc()
{
val = val + 10;
}
}
public class Child : Parent
{
public override void foo()
{
base.foo();
}
public override void inc()
{
val++;
}
}
static void Main(string[] args)
{
Parent p = new Child();
Console.WriteLine("p.val = " + p.val); //Output: p.val = 0
p.foo();
Console.WriteLine("P.val = " + p.val); //Output: p.val = 1
}
I am assuming the inc()
of the Parent class did not get called because {this}
pointer is actually pointing to a Child object so the Child's version of inc()
will be called from the Parent object's function foo()
. Is there a way to force the Parent's function foo()
to always call parent's function inc()
Like you could in C++ with ::
operator?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,非虚拟调用虚拟方法的唯一方法是使用
base.Foo
。当然,您可以在Parent
中编写一个非虚拟方法,并让Parent.foo()
调用该方法以及默认方法Parent.inc()
的实现。No, the only way you can call a virtual method non-virtually is with
base.Foo
. Of course, you could write a non-virtual method inParent
, and makeParent.foo()
call that, as well as the default implementation ofParent.inc()
.你这个问题想太多了。
如果您想要非虚拟调度,那么首先不要将方法设为虚拟。
如果您想要虚拟和非虚拟调度,那么创建两个方法,一个虚拟和一个静态
例如:
为工作使用正确的工具。如果您想要虚拟调度,则调用虚拟方法。如果您想要静态调度,则调用静态方法。不要试图对虚拟方法进行锤子攻击并使其静态分派;这违背了该工具的全部目的。
You're over-thinking the problem.
If you want non-virtual dispatch then don't make the methods virtual in the first place.
If you want both virtual and non-virtual dispatch then make two methods, one virtual and one static
For example:
Use the right tool for the job. If you want virtual dispatch then call a virtual method. If you want static dispatch then call a static method. Don't try to take a hammer to a virtual method and make it statically dispatched; that's working against the entire purpose of the tool.
Child 实例将调用它自己的类型实现。
foo()
调用base.foo()
和base.foo()
调用inc()
,其中本例inc()
来自 Child,因为实例是 Child 类型,并且将使用此实现。The Child instance will call its own type implementation.
foo()
callsbase.foo()
andbase.foo()
callsinc()
, which in this caseinc()
is from the Child, since the instance is Child type, and will use this implementation.嗯,实际上是可能的正如这里所说的:
这确实有效:
但这只是一个概念证明< /em>:这可怕并且完全破坏了多态性。
我认为你没有正当理由写这篇文章。
Well, it is actually possible as said here:
This does the trick:
But it's only a proof of concept: it's horrible and totally breaks the polymorphism.
I don't think you can have a valid reason to write this.