namespace _011_派生类的构造函数 {
class BaseClass
{
private int x;
protected int z;
public BaseClass()
{
Console.WriteLine("base class 无参构造函数");
}
public BaseClass(int x)
{
this.x = x;
Console.WriteLine("x赋值完成");
}
}
}
————————————————————————————————————————————————————————————
namespace _011_派生类的构造函数 {
class ClassXyz
{
public static int z;//静态字段
public static void TestMethod()
{
Console.WriteLine("这是静态方法");
}
private int x;
private int y;
}
}
————————————————————————————————————————————————————————————
namespace _011_派生类的构造函数 {
class DerivedClass:BaseClass
{
private int y;
public DerivedClass()//调用父类中无参的构造函数 当我们没有在子类的构造函数中显示声明调用父类的构造函数,默认会调用父类中的无参构造函数
{
Console.WriteLine("这个是DerivedClass 无参的构造函数");
}
public DerivedClass(int x, int y):base(x)
{
this.y = y;
base.z = 100;
Console.WriteLine("y赋值完成");
}
}
}
————————————————————————————————————————————————————————————
namespace _011_派生类的构造函数 {
class Program {
static void Main(string[] args) {//public private
//DerivedClass o1 = new DerivedClass();
//DerivedClass o2= new DerivedClass(1,2);
//BaseClass o1 = new BaseClass();
ClassXyz.z = 100;
Console.WriteLine(ClassXyz.z);
ClassXyz.TestMethod();
Console.ReadKey();
}
}
}