【发布时间】:2016-03-31 09:23:32
【问题描述】:
namespace AbstractImplementation
{
public class baseclass
{
public virtual void testingSealed()
{
Console.WriteLine("This is testingSealed base");
}
}
public class derived : baseclass
{
public override void testingSealed()
{
Console.WriteLine("This is testing derived");
}
}
class Program
{
static void Main(string[] args)
{
derived der = new derived();
der.testingSealed();
}
}
}
这将给出“这是测试派生”的输出。是否可以从 der.testingSealed() 获取“This is testingSealed base”?
【问题讨论】:
-
如果要获取基本消息,为什么不实例化基本类型呢?对我来说,这听起来像是XY 的问题。
-
要访问 baaeclass 方法,您必须创建基类的对象。在派生类中重写基类对象时,无法访问它。
-
这个问题是在一次采访中问我的。在这种情况下,我给出了 2 个答案。 1)如果我们要访问基类,不需要重写派生类中的方法 2)创建基类对象并访问该方法。但是他说有一些方法可以通过派生类对象访问在派生类中被覆盖的基类方法,当我问这个问题的答案时,他让我找出来。
-
对虚拟非密封方法使用
callMSIL 指令仅当调用实例为this时才被认为是可验证的,因此,它只能来自内部对象,而不是来自外部代码。
标签: c#