c#继承与构造函数

case:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    public class A
    {
        public A()
        {
            Console.WriteLine("A");
        }
        public A(int x)
        {
            Console.WriteLine("A+");
        }
    }
    public class B : A
    {
        public B():base(6)
        {
            Console.WriteLine("B");
        }
        public B( int x):this()
        {
            Console.WriteLine("B+");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B test = new B(4);
            Console.ReadKey();
        }
    }
}

  运行结果是:A+  B  B+

初始化是首先找到最顶的构造函数,一步步实现初始化。

posted @ 2013-04-14 21:41  CodingWang  阅读(116)  评论(0编辑  收藏  举报