C# 结构struct总结
2015-01-05 22:37
267 查看
C# 结构struct总结
1.结构是值类型,而且是密封的,不能继承和派生。
2.结构申明:
struct StructName
{
MemberDeclaration
}
struct Point
{
public int x ; //结构中字段初始化是不允许的
pub int y ;
}
3.结构具有以下特点
(1)结构与类非常类似,但是结构是值类型,类是引用类型。
(2)结构实例化可以不适用new运算符。但是在显示设置数据成员之后,才能调用他们的值。
(3)系统已经为结构提供一个隐式构造函数(无参数),所以结构申明构造函数必须是带参数的。
(4)结构不支持继承,不能结构派生其他结构。
(5)申明结构时,不允许在字段初始化时候赋值。
4.结构的编程例子
struct information
{
private string colour;
public string Colour
{
get { return colour; }
set { colour = value; }
}
private double hight;
public double Hight
{
set { hight = value; }
get { return hight; }
}
private string gender;
public string Gender
{
set { gender = value; }
get { return gender; }
}
public information(string colour, double hight, string gender)
{
//带有参数的构造函数,必须对所有变量赋值!!
this.colour = colour;
this.gender = gender;
this.hight = hight;
}
}
class Program
{
static void Main(string[] args)
{
information info = new information("red" , 180.1 , "Man");
information info1 = info;
Console.WriteLine("colour: {0}\nhight: {1}\ngender: {2} " ,info1.Colour , info1.Hight , info1.Gender);
}
}
1.结构是值类型,而且是密封的,不能继承和派生。
2.结构申明:
struct StructName
{
MemberDeclaration
}
struct Point
{
public int x ; //结构中字段初始化是不允许的
pub int y ;
}
3.结构具有以下特点
(1)结构与类非常类似,但是结构是值类型,类是引用类型。
(2)结构实例化可以不适用new运算符。但是在显示设置数据成员之后,才能调用他们的值。
(3)系统已经为结构提供一个隐式构造函数(无参数),所以结构申明构造函数必须是带参数的。
(4)结构不支持继承,不能结构派生其他结构。
(5)申明结构时,不允许在字段初始化时候赋值。
4.结构的编程例子
struct information
{
private string colour;
public string Colour
{
get { return colour; }
set { colour = value; }
}
private double hight;
public double Hight
{
set { hight = value; }
get { return hight; }
}
private string gender;
public string Gender
{
set { gender = value; }
get { return gender; }
}
public information(string colour, double hight, string gender)
{
//带有参数的构造函数,必须对所有变量赋值!!
this.colour = colour;
this.gender = gender;
this.hight = hight;
}
}
class Program
{
static void Main(string[] args)
{
information info = new information("red" , 180.1 , "Man");
information info1 = info;
Console.WriteLine("colour: {0}\nhight: {1}\ngender: {2} " ,info1.Colour , info1.Hight , info1.Gender);
}
}
相关文章推荐
- C#中结构(struct)与类(class)的区别
- 快速撑握C#知识点系列之(struct)结构
- C#DECIMAL结构取整方法总结
- 深入c# 类和结构的区别总结详解
- [转]C#之结构Struct
- C#面象对象学习:类(class)与结构(struct)
- C#中类(class)与结构(struct)的异同
- 【C#基础知识】之结构、数组及常用的几种排序方法总结
- C#之结构struct(转)
- C#结构(Struct)
- c#语言基础(6)----结构(struct)和类(class)的 区别
- C#基础之结构体(struct)
- C#中的struct(结构)为值类型,struct类型全接触
- 黑马程序员_学习日记3_C#基础归纳总结之结构
- 初步学习“C#中的结构(struct)”
- 初步学习“C#中的结构(struct)”
- C#中的结构(struct)
- C# 类(class)与结构(struct)的异同
- C# 结构Struct可以继承接口Interface
- 详说C#中的结构struct