gpt4 book ai didi

c# - 继承真的隐藏了方法吗?

转载 作者:太空宇宙 更新时间:2023-11-03 17:11:51 25 4
gpt4 key购买 nike

我收到以下编译器警告:

'Resources.Foo.GetType()' hides inherited member 'object.GetType()'. Use the new keyword if hiding was intended.    Resources.NET\Resources\Class1.cs   123 20  Resources

对于这个(非常简化的)代码:

public interface IFoo
{
int GetType();
string GetDisplayName();
}

public class Foo : IFoo
{
public int GetType() { return 3; } *** Warning: points to here (line 123)***
public string GetDisplayName() { return "Foo"; }
}

请注意,我不明白为什么 GetDisplayName() 函数没有收到警告。我显然忽略了一些东西。

我查了一下 Stack Overflow 并用谷歌搜索了这个错误,它似乎适用于类继承,而不适用于接口(interface)。我真的很困惑为什么这会被接口(interface)触发(它将其方法定义为虚拟的)。

提前感谢您的见解。

最佳答案

GetType方法在 System.Object 上定义class,是Framework所有类的最终基类。

此方法用于获取Type运行时的当前实例,我认为您不打算覆盖它(您有一个 int 返回类型)。

我建议您将该方法重命名为 GetFooType 或其他名称,或者实现您的接口(interface) explicitly以避免运行时问题。

关于c# - 继承真的隐藏了方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1151864/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com