您的位置:首页 > 其它

构造函数是否可被重写?

2013-04-20 19:02 211 查看
构造函数不能被继承,因此不能重写,但可以被重载。

View Code

public class Counter
{
// 构造函数可以被重载。
public Counter() { }
public Counter(string s) { }

public virtual void Test() { }
public static int currentCount;
public static int IncrementCount()
{
return ++currentCount;
}
}

public class Counter1 : Counter
{
public override void Test() { }
}

class TestCounter
{
static void Main()
{
// If you uncomment the following statement, it will generate
// an error because the constructor is inaccessible:
// Counter aCounter = new Counter();   // Error

Counter.currentCount = 100;
Counter.IncrementCount();
System.Console.WriteLine("New count: {0}", Counter.currentCount);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: