如何访问父类方法 [英] How to Access Parent class methods

查看:54
本文介绍了如何访问父类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用覆盖概念时访问父类方法



这是我的示例代码,但我想访问而不创建A a = new A()(Parent Main()中的类方法



  namespace 覆盖
{
class A
{
public A()
{
Console.WriteLine( A);
}
public virtual void 添加()
{
Console.WriteLine( A Add);
}
}
class B:A
{
public B()
{
Console.WriteLine( B< /跨度>);
}
public 覆盖 void 添加()
{
Console.WriteLine( B);
// base.Add();
}
}


class 计划
{
静态 void Main( string [] args)
{
A obj = new B();


obj.Add();
// 这里我想访问父类方法

Console.Read();
}
}
}





请帮助我。

解决方案

在提出这些问题之前至少阅读一些内容!基类的所有非私有方法都可以通过派生类(句点)直接访问。



-SA


阅读此链接

http://www.aspdotnet-suresh.com/2013/09/csharp-virtual-override-new-keywords-example.html#more [ ^ ]

How to access parent class methods while using overriding Concept

This is my sample code but i want to access without creating A a=new A()( Parent Class Method in Main())

namespace Override
{
    class A
    {
       public A()
        {
            Console.WriteLine("A ");
        }
       public virtual void Add()
       {
           Console.WriteLine("A Add");
       }
    }
    class B:A
    {
        public B()
        {
            Console.WriteLine("B");
        }
        public override void Add()
        {
            Console.WriteLine("B");
            //base.Add();
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            A obj = new B();
           
           
            obj.Add();
           //Here i want to access Parent Class Method  
            
            Console.Read();
        }
    }
}



Please help me.

解决方案

Read at least something before asking such questions! All non-private methods of a base class are directly accessible by a derived class, period.

—SA


Read this link
http://www.aspdotnet-suresh.com/2013/09/csharp-virtual-override-new-keywords-example.html#more[^]


这篇关于如何访问父类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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