为什么覆盖在C#alowed静态方法 [英] Why is overriding static method alowed in C#

查看:150
本文介绍了为什么覆盖在C#alowed静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected static new void WhyIsThisValidCode()
{
}

你为什么允许覆盖静态方法?
无非是错误可能来自它,它doensn't工作,你想。

Why are you allowed to override static methods? Nothing but bugs can come from it, it doensn't work as you would think.

看看下面的类。

class BaseLogger
{
    protected static string LogName { get { return null; } }

    public static void Log(string message) { Logger.Log(message, LogName); }
}

class SpecificLogger : BaseLogger
{
    protected static string LogName { get { return "Specific"; } }
}

这是alowed,代码

this is alowed, and the code

SpecificLogger.Log("test");

时,altso alowed,但它不会做什么,你会觉得通过看代码

is altso alowed, but it doesn't do what you would think by looking at the code.

它调用 Logger.Log LOGNAME = NULL

那么,为什么这可以吗?

So why is this allowed?

推荐答案

关键字不覆盖的方法。它代替创建这是独立于原始的相同名称的新方法。这是不可能覆盖一个静态方法,因为他们不是虚拟的。

The new keyword does not override a method. It instead creates a new method of the same name which is independent of the original. It is not possible to override a static method because they are not virtual

这篇关于为什么覆盖在C#alowed静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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