C#关键字之:base、this

构造函数后加:this() 
public class cstus
    {
        private int age;

  
        public cstus(string a) { }

        public cstus()
         {
             this.age = 25;
         }

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
      
    }

   cstus c = new cstus("");
    int age = c.Age;
  这个时候 age =0


修改cstus如下:

 public class cstus
    {
        private int age;

  
        public cstus(string a):this() { }

        public cstus()
         {
             this.age = 25;
         }

        public int Age
        {
            get { return age; }
            set { age = value; }
        }
      
    }
   cstus c = new cstus("");
    int age = c.Age;
   这个时候 age =25

构造函数后加:base()   base 关键字用于从派生类中访问基类的成员
using System;
  public class BaseClass
  {
  int num;
  public BaseClass()
  {
  Console.WriteLine("in BaseClass()");
  }
  public BaseClass(int i)
  {
  num = i;
  Console.WriteLine("in BaseClass(int {0})", num);
  }
  }
  public class DerivedClass : BaseClass
  {
  // 该构造器调用 BaseClass.BaseClass()
  public DerivedClass() : base()
  {
  }
  // 该构造器调用 BaseClass.BaseClass(int i)
  public DerivedClass(int i) : base(i)
  {
  }
  static void Main()
  {
  DerivedClass dc = new DerivedClass();
  DerivedClass dc1 = new DerivedClass(1);
  }
  }
  /**//*

posted @ 2008-07-03 16:38  hemingchen  阅读(265)  评论(0编辑  收藏  举报