class A { public string name = "张三"; public A() { this.Print(); } public virtual void Print() { A a = this; Console.Write("A: " + a.name); } public void SayHi() { Console.WriteLine("A: Hi"); } } class B:A { public new string name = "李四";//new关键字使此处的name作为B的属性覆盖了继承自A的name属性 public B() { this.name = "王五"; this.Print(); } public override void Print() { A a = this; Console.Write("B: " + a.name); } public new void SayHi() { Console.WriteLine("B: Hi"); } } class Program { static void Main(string[] args) { /* * new B() 过程 * 1、创建新对象 * 2、将构造函数的作用域赋给新对象(因此this就指向了这个新对象) * 3、执行构造函数中的代码(先执行基类构造函数中的代码) * 4、返回新对象 */ B b = new B(); //B: 张三B: 张三 A a = new A(); List<A> list = new List<A>{a, b}; foreach(A o in list) { o.SayHi();//A: Hi } Console.ReadLine(); } }