这是一个很难解释的问题,但我会尽我所能。我已经实例化了一个存储在A类型变量中的类型为B的对象。我使用get类型属性,所以现在它是类型B.因此,我执行了A和B类型的隐式转换。 show()被调用是它的类型B?C#类型转换
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class A
{
public virtual void show()
{
Console.WriteLine("Showing A");
}
public void test()
{
Console.WriteLine("called from A");
}
}
class B:A
{
public override void show()
{
Console.WriteLine("Showing B");
}
public void testb()
{
Console.WriteLine("called from B");
}
}
class Program
{
static void Main(string[] args)
{
A a = new B();
// Outputs ConsoleApplication.B
Console.WriteLine("{0}", a.GetType());
// outputs showing B
a.show();
// outputs called from A
a.test();
Console.ReadLine();
}
}
}
您是否知道您命名方法testb?这可能是一个错字。 – Joe 2012-01-31 21:45:43