为什么程序启动后会显示C::Foo(object o)
?
using System;
namespace Program
{
class A
{
static void Main(string[] args)
{
var a = new C();
int x = 123;
a.Foo(x);
}
}
class B
{
public virtual void Foo(int x)
{
Console.WriteLine("B::Foo");
}
}
class C : B
{
public override void Foo(int x)
{
Console.WriteLine("C::Foo(int x)");
}
public void Foo(object o)
{
Console.WriteLine("C::Foo(object o)");
}
}
}
我不明白为什么当你调用 C::Foo
时,使用 object
选择方法,而不是使用 int
。 B
类是什么,该方法被标记为覆盖?
C
类中,有两个同名不同参数的方法,是不是重载了?为什么不?在父级中覆盖其中一种方法是否重要?它以某种方式禁用过载?
最佳答案
First, the set of all accessible (Section 3.5) members named N declared in T and the base types (Section 7.3.1) of T is constructed. Declarations that include an override modifier are excluded from the set. If no members named N exist and are accessible, then the lookup produces no match, and the following steps are not evaluated.
所以根据这个,它会使用
public void Foo(object o)
首先
关于c# - 为什么重载不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718706/