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.");
}
}
}
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.");
}
}
}
相关文章推荐
- 关于C#中派生类调用基类构造函数的理解 base使用
- SqlServer 多条件查询 [存储过程] 经典例子结合C#的使用
- C#中基类和派生类的构造函数以及变量的执行顺序整理
- C#皮肤使用例子.
- C# 构造函数 例子
- CodeProject - 在C#使用SHGetFileInfo获取(管理)文件或者文件夹图标(C#封装Win32函数的一个例子)
- extern外部方法使用C#简单例子
- 派生类构造函数与析构函数例子(模拟对话框)
- C# Wke使用例子 (KyozyWke)
- C#存储过程使用例子
- c#学习笔记之九 事件(event)的使用,一个简单的例子揭示其设计理念
- C#中静态构造函数含义及使用
- C# 使用存储过程访问数据库的完整例子
- 派生类构造函数的定义和使用举例
- [C#]使用RabbitMQ模拟抽奖系统的例子
- c#中静态构造函数以及例子
- c#中抽象类、接口的使用例子
- C#使用GMAIL群发带附件邮件的例子
- 详解C#编程中构造函数的使用
- .net C# 反射技术使用例子(转自:http://www.programbbs.com/doc/5740.htm)