简体   繁体   English

如果父子 class 具有同名的 static 方法,为什么会认为方法隐藏?

[英]Why is it considered method hiding if a parent and child class have a static method with the same name?

If you declare an inheritance hierarchy where both the parent and child class have a static method of the same name and parameters*, Visual Studio will raise warning CS0108 :如果您声明 inheritance 层次结构,其中父子 class 具有相同名称和参数的 static 方法*,Visual Studio 将引发警告CS0108

Example:例子:

public class BaseClass
{
    public static void DoSomething()
    {
    }
}

public class SubClass : BaseClass
{
    public static void DoSomething()
    {
    }
}

: warning CS0108: 'SubClass.DoSomething()' hides inherited member 'BaseClass.DoSomething()'. Use the new keyword if hiding was intended.

Why is this considered method hiding?为什么这个被认为是隐藏的方法? Neither method is involved in the inheritance hierarchy and can only be invoked by using the class name:这两种方法都不涉及 inheritance 层次结构,只能使用 class 名称调用:

BaseClass.DoSomething();
SubClass.DoSomething();

or, unqualified in the class itself.或者,class 本身不合格。 In either case, there is no ambiguity as to which method is being called (ie, no 'hiding').在任何一种情况下,调用哪个方法都没有歧义(即,没有“隐藏”)。

*Interestingly enough, the methods can differ by return type and still generate the same warning. *有趣的是,这些方法可能因返回类型而异,但仍会产生相同的警告。 However, if the method parameter types differ, the warning is not generated.但是,如果方法参数类型不同,则不会生成警告。

Please note that I am not trying to create an argument for or discuss static inheritance or any other such nonsense.请注意,我并不是要为 static inheritance 或任何其他此类废话创建论据或讨论。

Why is this considered method hiding?为什么这个被认为是隐藏的方法? Neither method is involved in the inheritance hierarchy and can only be invoked by using the class name. inheritance 层次结构中不涉及这两种方法,并且只能使用 class 名称调用。

That is not true.那不是真的。 You can call DoSomething from any inherited class name:您可以从任何继承的 class 名称调用 DoSomething:

public Class A
{
     public static void C() {...}
}

public Class B: A
{

}

B.C() // Valid call!

That is why you are hiding C() if you declare the method with the same signature in B.这就是为什么在 B 中声明具有相同签名的方法时隐藏 C() 的原因。

Hope this helps.希望这可以帮助。

Members of the SubClass will not be able to access the DoSomething from BaseClass without explicitly indicating the class name.如果不明确指出 class 名称,子类的成员将无法从 BaseClass 访问 DoSomething。 So it is effectively "hidden" to members of SubClass, but still accessible.所以它实际上对子类的成员“隐藏”,但仍然可以访问。

For example:例如:

public class SubClass : BaseClass
{
    public static void DoSomething()
    {
    }

    public static void DoSomethingElse()
    {
        DoSomething(); // Calls SubClass
        BaseClass.DoSomething(); // Calls BaseClass
    }
}

It's just a warning.这只是一个警告。 The compiler just wants to make sure you intentionally used the same method name.编译器只是想确保您有意使用相同的方法名称。

Visual Studio, and Philippe, are saying it's a warning so your code will compile and run. Visual Studio 和 Philippe 表示这是一个警告,因此您的代码将编译并运行。

However, 'CodeNaked' nicely demonstrates why it is hidden.然而,“CodeNaked”很好地展示了它为什么被隐藏。 This code compiles without throwing errors or warnings.此代码编译时不会抛出错误或警告。 Thanks to 'CodeNaked'感谢'CodeNaked'

public class BaseClass {
    public virtual void DoSomething() {
    }
}

public class SubClass : BaseClass {
    public override void DoSomething() {

    }

    public void DoSomethingElse() {
        DoSomething(); // Calls SubClass
        base.DoSomething(); // Calls BaseClass
    }
}

EDIT: With Travis's code I can do the following:编辑:使用 Travis 的代码,我可以执行以下操作:
BaseClass.DoSomething(); BaseClass.DoSomething();
SubClass.DoSomething();子类.DoSomething();

And it works fine.它工作正常。 Thing is though you might wonder why SubClass is inheriting from BaseClass and both are implementing the same static methods.事情是你可能想知道为什么 SubClass 继承自 BaseClass 并且两者都实现了相同的 static 方法。 Actually that's not true, both classes are implementing methods that could be completely different but have the same name.实际上这不是真的,两个类都实现了可能完全不同但名称相同的方法。 That could be potential confusing.这可能会令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 接口继承-从父接口隐藏方法-为什么在实现类中可以同时使用这两种方法? - Interface inheritance - hiding method from parent interface - why can I have both methods in the implementing class? 什么子类导致静态父方法? 还是不可能完成任务? - What child-class caused static parent method? Or mission impossible? 为什么单独的方法调用被认为是相同的任务? - Why are separate method invocations considered the same Task? 如何从具有相同名称的父级和子级扩展方法 - How to extend method from parent and child with the same name 在子类中使用非泛型方法“隐藏”泛型方法 - “Hiding” generic methods with nongeneric method in child class 静态方法依赖于子类? - Static method dependent on child class? 当在类中声明相同名称实例级静态字段时,为什么方法范围参数值被搞乱 - Why a method scope parameter value is messed up when the same name instance level static field is declared in a class 在父类上调用静态方法时,父类可以推断子类(C#)的类型吗? - When calling a static method on parent class, can the parent class deduce the type on the child (C#)? 在父方法中访问子级的静态属性 - Accessing a static property of a child in a parent method 父级方法中的静态子字段 - Static child field in parent's method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM