C# 关键字base用法

1.调用基类的方法

public class A
{
        public virtual void Hello()
        {
                Console.WiriteLine("Hello");
        }
}
public class B : A
{
        public override void Hello()
        {               
                base.Hello();//调用基类的方法,显示Hello
                Console.WiriteLine("World");
        }
}

2.搭配构造函数执行基类的有参无参构造函数

public class A
{
        public A()
        {
                Console.WriteLine("Build A");
        }
        public A(string name)
        {
                Console.WriteLine(name);
        }
}
public class B:A
{
        public B(string name):base(name)
        {
                Console.WriteLine("Build B");
        }
        static void Main()
        {
                //会先执行基类public A(string name),然后再执行public B(string name)
                B b = new B("Hello World");
                Console.ReadLine();
        }
}    

 

posted @ 2019-01-22 11:16  木狼  阅读(376)  评论(0编辑  收藏  举报