转自:http://wenku.baidu.com/view/e8217edb6f1aff00bed51e36.html
结构体的定义:
结构体也可以象类一样可以单独定义.
class a{};
struct a{};
结构体也可以在名字前面加入控制访问符.
public struct student{};
internal struct student{};
如果结构体student没有publice或者internal的声明 类program就无法使用student结构定义 obj对象
如果结构体student的元素没有public的声明,对象obj就无法调用元素x
因为默认的结构体名和元素名是private类型
程序:
using System;
public struct student
{
public int x;
};
class program
{
public static void Main()
{
student obj=new student();
obj.x=100;
}
};
在结构体中也可以定义静态成员与类中一样,使用时必须用类名,或结构名来调用不属于实例,声明时直接定义.
程序:
using System;
public struct student
{
public static int a = 10;
};
class exe
{
public static void Main()
{
Console.WriteLine( student.a = 100);
}
};
或
using System;
class base
{
public struct student
{
public static int a = 10;
};
}
class exe
{
public static void Main()
{
Console.WriteLine( base.student.a = 100);
}
};
在结构体中可以定义构造函数以初始化成员,但不可以重写默认无参构造函数和默认无参析构函数
程序:
public struct student
{
public int x;
public int y;
public static int z;
public student(int a,int b,int c)
{
x=a;
y=b;
student.z=c;
}
};
在结构体中可以定义成员函数。
程序:
public struct student
{
public void list()
{
Console.WriteLine("这是构造的函数");
}
};
结构体的对象使用new运算符创建(obj)也可以直接创建单个元素赋值(obj2)这是与类不同的因为类只能使用new创建对象
程序:
public struct student
{
public int x;
public int y;
public static int z;
public student(int a,int b,int c)
{
x=a;
y=b;
student.z=c;
}
};
class program
{
public static void Main()
{
student obj=new student(100,200,300);
student obj2;
obj2.x=100;
obj2.y=200;
student.z=300;
}
}
在使用类对象和函数使用时,使用的是引用传递,所以字段改变
在使用结构对象和函数使用时,是用的是值传递,所以字段没有改变
程序:
using System;
class class_wsy
{
public int x;
}
struct struct_wsy
{
public int x;
}
class program
{
public static void class_t(class_wsy obj)
{
obj.x = 90;
}
public static void struct_t(struct_wsy obj)
{
obj.x = 90;
}
public static void Main()
{
class_wsy obj_1 = new class_wsy();
struct_wsy obj_2 = new struct_wsy();
obj_1.x = 100;
obj_2.x = 100;
class_t(obj_1);
struct_t(obj_2);
Console.WriteLine("class_wsy obj_1.x={0}",obj_1.x);
Console.WriteLine("struct_wsy obj_2.x={0}",obj_2.x);
Console.Read();
}
}
结果为:class_wsy obj_1.x=90
struct_wsy obj_2.x=100
说说struct。对于struct,在一定程度上与class非常相像,分析一下:
class program
{
static void Main(string[] args)
{
STR str1;
str1.Method("str1");//第一种方法,直接定义
STR str2 = new STR();
str2.Method("str2"); //第二种方法,通过new关键字
}
}
struct STR
{
public void Method(string Par)
{
Console.WriteLine("参数是:"+Par);
}
}
一个是直接定义来调用,也可以通过new关键字来定义调用,这是与类不同的,类必需经过实例化(new)来定义调用(静态成员是通过类名调用,没有定义)。
接下来再看一个例子。
class program
{
static void Main(string[] args)
{
STR str1;//第一种
str1.count = 100;//赋值
Console.WriteLine(str1.count);
STR str2 = new STR();//第二种
Console.WriteLine(str2.count);
}
}
struct STR
{
public int count;
}
当结构体中定义一个全局变量时(类中叫字段),这个count是没有值的,这是与类不同的,在类中,所有的字段都有默认值,结构中是没有的。既然没有默认值,那在第二种实例化直接调用时,str2.count输出的为什么是0,这个0是没有事前赋值的。如果是类,我们知道,类是有构造函数的,如果我们不显式写上构造函数,CLR会自动给我们加上一个没有参数的构造函数的,并且这个构造函数是可以被显式的写出来的。同样,结构也是有一个构造函数,但这个构造函数是不能够写出来的,并且这个构造函数很特别,从第二种就能看出,只要用new关键字来实例化结构,就会把count给初始化成0,也就是那个不能写出来的无参构造函数会初始化所有的结构中的全局变量。对于第一种,因为没有调用无参的构造函数,所以必需去显式的给结构中的全局变量去赋值。
如果结构里有有参构造函数,代码如下:
class program
{
static void Main(string[] args)
{
STR str = new STR("");//实例化
Console.WriteLine(str.count);
}
}
struct STR
{
public int count;
public STR(string str)
{
count = 1;
}
}
会发现,如果是个有参构造函数,在构造中必需去初始化结构中的全局变量,因为如果用有参构造函数的话,无参的就不会得到调用,结构中的全局变量就得不到初始化,所以必需在有参构造函数中去初始化它。如果有多个全局变量,都得在这个有参构造函数中去实例化它们。