C# 继承中的构造函数

1、当声明一个子类对象时,会先执行父类的构造函数,再执行子类构造函数
2、子类可以通过base 关键字 代表父类,调用父类构造

    public class Father
    {
        public Father( int i )
        {
            Console.WriteLine( "父类构造函数"+i );
        }
    }
    public class Son : Father
    {
        public Son( int i ) : base( i ) 
        {
            Console.WriteLine("子类构造函数" + i);
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Son testson = new Son(5);
        }
    }

输出:
父类构造函数5
子类构造函数5

你可能感兴趣的:(c#,开发语言)