是否可以通过实例化派生类对象来访问基类虚方法,即使在派生类中重写了相同的虚方法? [英] Is it possible to access base class virtual method by instantiating derived class object, even though the same virtual method is overridden in the derived class?

查看:57
本文介绍了是否可以通过实例化派生类对象来访问基类虚方法,即使在派生类中重写了相同的虚方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 命名空间 AbstractImplementation 
{
class 计划
{
静态 void Main( string [] args)
{
derived der = new derived();
der.testingSealed();

}
}

public class baseclass
{
public virtual void testingSealed()
{
Console.WriteLine( 这是testingSealed base);
}
}



public class 派生:baseclass
{
public 覆盖 void testingSealed()
{
Console.WriteLine( 这是测试派生的);
}

}
}





//此方法将输出为这是测试派生的。是否可以访问基类方法testingSealed()。我想为 ( 这是testingSealed碱基 ) 的输出为der.testingSealed()。

解决方案
的方法得到的,它是 base.testingSealed()。当然,方法名称具有误导性:这里没有任何内容。 :-)



-SA


namespace AbstractImplementation
{
    class Program
    {
        static void Main(string[] args)
        {
            derived der = new derived();
            der.testingSealed();  
 
        }
    }

    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");
        }
  
    }
}



//This method will give output as "This is testing derived".Is it possible to acess baseclass method testingSealed(). I want the output for der.testingSealed() as "("This is testingSealed base")".

解决方案

In a method of derived, it's base.testingSealed(). Of course, the method name is misleading: nothing is sealed here. :-)

—SA


这篇关于是否可以通过实例化派生类对象来访问基类虚方法,即使在派生类中重写了相同的虚方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆