您的位置:首页 > 编程语言 > C#

C#的 派生类 构造函数使用 例子。

2007-03-10 10:03 253 查看
//zhangzhicong 2007-03-10

using System;
using System.Collections.Generic;
using System.Text;

namespace t070309
{
class Program
{
static void Main(string[] args)
{
b bb = new b();
Console.ReadKey();
}
}

public class a
{
//无参数,最好要定义一个无参数的构造函数,这是很好的习惯
public a()
{
Console.WriteLine("a init null");
}

//一个参数
public a(string m)
{
Console.WriteLine("a init m={0}.", m);
}

//两个参数
public a(string m,string n )
{
Console.WriteLine("a init m={0},n={1}.", m,n);
}
}

public class b : a
{
private static string m = "zhangzhicong";
private static string n = "man";

/*
* 说明:类A已经自定义了 无参数构造函数,即在调用基类的构造函数,
* 问题在于,基类的默认构造函数已经不存在,即要调用基类的自定义
* 构造函数了,这里使用了base(),在这里确定要调用的构造函数
* */
public b():base()
{
Console.WriteLine("b init null.");
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: