/**//*--===------------------------------------------===---
--===------------------------------------------===---*/
using System;
namespace xumh
{
public class myClass
{
public myClass()//无参构造器,不得少; 类里面若有带参构造器,必须有无参构造器
{
Console.WriteLine("myClass Constructor");
}
public myClass(string name)
{
Console.WriteLine("hi,I'm in myClass -" + name);
}
}
public class yourClass:myClass
{
public yourClass()
{
Console.WriteLine("your class constructor");
}
public yourClass(string name)
{
Console.WriteLine("hello,"+ name);
}
};
public class runMyApp
{
static void Main()
{
yourClass your1 = new yourClass();
yourClass your2 = new yourClass("xuminghui");
Console.WriteLine("hello,world.");
}
}
}
--===------------------------------------------===---*/
using System;
namespace xumh
{
public class myClass
{
public myClass()//无参构造器,不得少; 类里面若有带参构造器,必须有无参构造器
{
Console.WriteLine("myClass Constructor");
}
public myClass(string name)
{
Console.WriteLine("hi,I'm in myClass -" + name);
}
}
public class yourClass:myClass
{
public yourClass()
{
Console.WriteLine("your class constructor");
}
public yourClass(string name)
{
Console.WriteLine("hello,"+ name);
}
};
public class runMyApp
{
static void Main()
{
yourClass your1 = new yourClass();
yourClass your2 = new yourClass("xuminghui");
Console.WriteLine("hello,world.");
}
}
}
问题:输出什么?
myClass Constructor
your class constructor
myClass Constructor
hello,xuminghui
hello,world.
还要注意以下:
静态构造函数:又叫类型初始化器,用于初始化静态成员.静态构造函数不得有参数,默认是private修饰符且不得改变,不得在代码中写private.