C#基础篇 结构:在结构中定义构造函数
2014-01-10 11:28
162 查看
结构具有以下特点:
结构是类型,而类是引用类型与类不同,结构的实例化可以不使用new运算符
结构可以声明构造函数,但是它必须带有参数
一个结构不能从另一个结构或者类继承,而且不能作为其他结构或者类的基类
结构可以实现接口
结构可以用null值对结构成员进行赋值
在实例化时,可以通过new关键字进行实例化,也可以不通过new关键字进行实例化
struct ABC{
public string Name;
}
ABC wm = new ABC(); //通过new关键字实例化
Console.WriteLine(wm.Name); //输出为空
/****************************/
ABC wm2;
Console.WriteLine(wm2.Name); //错误!!!
/**************************/
在结构中只能创建带有参数的构造函数:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace wm110_1 { class Program { struct CoConfiguration { public CoConfiguration(string Name, Object Leaguer, string Address, int Phone) //定义构造函数 { CoName = Name; CoLeaguer = Leaguer; CoAddress = Address; CoPhone = Phone; } public string CoName; public Object CoLeaguer; public string CoAddress; public int CoPhone; } struct Leaguer { public Leaguer(string M, string BM, string E) //定义构造函数 { Mgr = M; BranchMgr = BM; Employee = E; } public string Mgr; public string BranchMgr; public string Employee; } static void Main(string[] args) { Leaguer leaguer = new Leaguer("赛某","刘某","孙某"); //实例化同时,通过构造函数赋值 CoConfiguration coconf = new CoConfiguration("微软",leaguer,"西安",123456); //用new关键字进行实例化 string branchMgr = ((Leaguer)coconf.CoLeaguer).BranchMgr; //获取子结构中的成员值 Console.WriteLine(coconf.CoName +"公司,部门经理:"+branchMgr); } } }
相关文章推荐
- 17、C#中的常量和结构的定义与使用
- C#==>用户定义类(或结构)的类型转换
- 17、C#中的常量和结构的定义与使用
- 如何在C#中定义动态的数组结构啊??
- C#中如何定义缺省参数的构造函数
- C#基础(七)构造函数、结构
- .NET/C#中的结构中的静态成员及静态构造函数
- c的结构在c#里的定义方法
- C# 类的定义及构造函数的重载
- C#温故而知新学习系列之面向对象编程—定义结构(二)
- C#温故而知新学习系列之面向对象编程—2-定义结构
- C#编程:枚举(enum)与结构(struct)的定义和使用方法
- c#的结构类型也可以有构造函数
- 在解析C#结构体指针前,必须知道C#结构体是如何定义的。在c#中同样定义该结构体。
- c#的结构类型也可以有构造函数
- C#中的自定义结构转换操作符的定义方法
- 在C#中如何定义一个变长的结构数组?如果定义好了,如何获得当前数组的长度?
- C#中结构数组怎么定义?怎么获取一个字符的ASCII码?
- c# 类的构造函数的默认定义和个性化定义
- 【第二篇日记】C# 的转义字符,结构定义、字符转换、类型检查、运算符以及正则表达式