class A
{ int x=2;//when new B() in the program, this would be implemented after the 2 before the 3 public A()//3. {
PrintFields();//4. begin to invoke overrided PrintFields() } publicvirtualvoid PrintFields()//根本不会被执行到 {
}
} class B:A
{ int x=1;//1. x=1 mean while y=0 int y;
privatestring str="";//this would be implemented before 2 after 1 public B()//2. begin to invoke A's constructor after 5th step back to here so it's 6th step, too. {
y=-1;
} publicoverridevoid PrintFields()//5. {
Console.WriteLine("x={0},y={1}",x,y);
}
}
class Program
{ publicstaticvoid Main(string[] args)
{ //继承的子类在new时,先初始化自身的字段,再初始化父类字段,再执行父类构造器,最后执行自身的构造器,此时在此例中“已执行输出语句” B b =new B();//start
Console.WriteLine("Hello World!");
// TODO: Implement Functionality Here
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
posted on
2009-04-29 21:43simhare
阅读(121)
评论(0)
收藏举报