是否有可能重写非虚方法? [英] Is it possible to override a non-virtual method?

查看:386
本文介绍了是否有可能重写非虚方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法来覆盖非虚方法?或东西,让类似的结果(不是创建一个新方法等来调用所需的方法)?

Is there any way to override a non-virtual method? or something that gives similar results (other than creating a new method to call the desired method)?

我想覆盖从 Microsoft.Xna.Framework.Graphics.GraphicsDevice 的方法在考虑单元测试。

I would like to override a method from Microsoft.Xna.Framework.Graphics.GraphicsDevice with unit testing in mind.

推荐答案

没有,你不能忽略非虚方法。你能做的最接近的事是隐藏通过创建一个方法同名的方法,但这是不可取的,因为它打破了良好的设计原则。

No, you cannot override a non-virtual method. The closest thing you can do is hide the method by creating a new method with the same name but this is not advisable as it breaks good design principles.

但是,即使隐藏的方法不会给你的执行时间的方法态分派调用,例如将一个真实的虚拟方法调用。考虑这个例子:

But even hiding a method won't give you execution time polymorphic dispatch of method calls like a true virtual method call would. Consider this example:

using System;

class Example
{
    static void Main()
    {
        Foo f = new Foo();
        f.M();

        Foo b = new Bar();
        b.M();
    }
}

class Foo
{
    public void M()
    {
        Console.WriteLine("Foo.M");
    }
}

class Bar : Foo
{
    public new void M()
    {
        Console.WriteLine("Bar.M");
    }
}

在这个例子中两个呼叫到 M 方法打印 Foo.M 。正如你所看到的这种做法确实让你有一个方法,一个新的实现,只要参照该对象是正确的派生类型,但隐藏的基础方法的确实的突破多态性。

In this example both calls to the M method print Foo.M. As you can see this approach does allow you to have a new implementation for a method as long as the reference to that object is of the correct derived type but hiding a base method does break polymorphism.

我会建议你不要躲在这种方式的基础方法。

I would recommend that you do not hide base methods in this manner.

我倾向于与那些谁赞成C#的默认行为方法都是非虚默认情况下(而不是Java的)到另一边。我会再进一步​​说,类应该也被默认密封。继承是很难设计的正确和事实,那就是未标记是虚拟的方法表明,该方法从未用于该方法的作者被覆盖。

I tend to side with those who favor C#'s default behavior that methods are non-virtual by default (as opposed to Java). I would go even further and say that classes should also be sealed by default. Inheritance is hard to design for properly and the fact that there is a method that is not marked to be virtual indicates that the author of that method never intended for the method to be overridden.

编辑:执行时间多态分派

我的意思是,在执行时,当你调用虚方法发生的默认行为。比方说,例如,在我的previous code例如,而不是定义一个非虚方法,我做实际上定义了一个虚拟的方法和一个真正的重写的方法为好。

What I mean by this is the default behavior that happens at execution time when you call virtual methods. Let's say for example that in my previous code example, rather than defining a non-virtual method, I did in fact define a virtual method and a true overridden method as well.

如果我是在这种情况下调用 b.Foo 时,CLR会正确地确定对象的类型的 B 的参考点为酒吧和将派调用 M 恰如其分。

If I were to call b.Foo in that case, the CLR would correctly determine the type of object that the b reference points to as Bar and would dispatch the call to M appropriately.

这篇关于是否有可能重写非虚方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆