using System;namespace Test{ //父类若没有默认构造,子类必须在其构造函数内显式调用父类的带参构造才能保证子类对象的顺利实例化! public class MyClass { static MyClass() { Console.WriteLine("MyClass:默认静态构造函数"); } public MyClass() { Console.WriteLine("MyClass:默认构造函数"); } public MyClass(int a, int b) { Console.WriteLine("MyClass带参构造:a={0}, b={1}.", a, b); } } public class MyClass2 : MyClass { static MyClass2() { Console.WriteLine("MyClass2:默认静态构造函数"); } public MyClass2() { Console.WriteLine("MyClass2:默认构造函数"); } public MyClass2(int a, int b) { Console.WriteLine("MyClass2带参构造:a={0}, b={1}.", a, b); } } public class MyClass3 : MyClass2 { static MyClass3() { Console.WriteLine("MyClass3:默认静态构造函数"); } public MyClass3() { Console.WriteLine("MyClass3:默认构造函数"); } public MyClass3(int a, int b) { Console.WriteLine("MyClass3带参构造:a={0}, b={1}.", a, b); } } public class runMyApp { static void Main() { MyClass3 my = new MyClass3(3, 4); } }}
新闻热点
疑难解答