struct 的用法

来源:互联网 发布:约瑟夫问题c语言 编辑:程序博客网 时间:2024/05/02 03:10

 

 

 

 

|

首先结构是值类型。

结构是使用 struct 关键字定义的,结构如下:

struct 结构名
{
}

结构概述

结构具有以下特点:

  • 结构是值类型,而类是引用类型。 (结构不能包含显式的无参数构造函数)

  • 与类不同,结构的实例化可以不使用 new 运算符。

  • 结构可以声明构造函数,但它们必须带参数。

  • 一个结构不能从另一个结构或类继承,而且不能作为一个类的基。所有结构都直接继承自 System.ValueType,后者继承自 System.Object

  • 结构可以实现接口。

  • 结构在定义变量时不能给定初始值。(另加)

  • 如果要在结构中使用构造函数则必须给所有的变量赋值(在构造函数中直接给变量赋值而不是给变量的属性赋值,因为在未赋值之前属性是没有值的所以不能直接给属性)(另加)

  • 所有的结构都隐式继承自ValueType类,不需要显示指定;。(另加)

  • 结构的继承列表中只允许有接口。(另加)

  • 结构的继承是三层的:object >> valuetype >> "结构" 。(另加)

例:(自己写的,描述员工信息,姓名、年龄、工资)

    //结构,结构是值类型的
    //结构在定义变量时不能给定初始值
    struct Employeestruct
    {
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int age;
        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        private int gongzhi;
        public int Gongzhi
        {
            get { return gongzhi; }
            //set { gongzhi = value; }
        }
        //有参数构造函数
        public Employeestruct(string _name, int _age, int _gongzhi)
        {
            //如果要在结构中使用构造函数则必须给所有的变量赋值(在构造函数中赋值)
            this.name = _name;
            this.age = _age;
            this.gongzhi = _gongzhi;
        }
    }

    //使用结构

    class Program
    {
        static void Main(string[] args)
        {

            ////实例化Employeestruct结构
            //Employeestruct empstruct = new Employeestruct("Steven", 22, 10000);
            //Console.WriteLine("姓名:{0}\n年龄:{1}\n工资:{2:C2}", empstruct.Name, empstruct.Age, empstruct.Gongzhi);
            Console.Read();
        }
    }

使用结构(C# 编程指南)

 

struct 类型适于表示 PointRectangle 和 Color 等轻量对象。尽管可以将一个点表示为,但在某些情况下,使用结构更有效。例如,如果声明一个 1000 个 Point 对象组成的数组,为了引用每个对象,则需分配更多内存;这种情况下,使用结构可以节约资源。由于 .NET Framework 包含名为 Point 的对象,因此我们转而调用结构“CoOrds”。

public struct CoOrds
{
    public int x, y;
    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}

声明结构的默认(无参数)构造函数是错误的。总是提供默认构造函数以将结构成员初始化为它们的默认值。在结构中初始化实例字段也是错误的。

如果使用 new 运算符创建结构对象,则会创建该结构对象,并调用适当的构造函数。与类不同,结构的实例化可以不使用 new 运算符。如果不使用new,则在初始化所有字段之前,字段都保持未赋值状态且对象不可用。

对于结构,不像类那样存在继承。一个结构不能从另一个结构或类继承,而且不能作为一个类的基。但是,结构从基类 Object 继承。结构可实现接口,其方式同类完全一样。

与 C++ 不同,无法使用 struct 关键字声明类。在 C# 中,类与结构在语义上是不同的。结构是值类型,而类是引用类型。有关更多信息,请参见值类型

除非需要引用类型语义,否则系统将较小的类作为结构处理效率会更高。

示例 1

下面的示例演示使用默认构造函数和参数化构造函数的 struct 初始化。

public struct CoOrds
{
    public int x, y;
    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}
// Declare and initialize struct objects.
class TestCoOrds
{
    static void Main()
    {
        // Initialize:   
        CoOrds coords1 = new CoOrds();
        CoOrds coords2 = new CoOrds(10, 10);

        // Display results:
        System.Console.Write("CoOrds 1: ");
        System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);

        System.Console.Write("CoOrds 2: ");
        System.Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
    }
}
输出

CoOrds 1: x = 0, y = 0

CoOrds 2: x = 10, y = 10

 

示例 2

下面举例说明了结构特有的一种功能。它在不使用 new 运算符的情况下创建 CoOrds 对象。如果将 struct 换成 class,程序将不会编译。

public struct CoOrds
{
    public int x, y;
    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}

// Declare a struct object without "new."
class TestCoOrdsNoNew
{
    static void Main()
    {
        // Declare an object:
        CoOrds coords1;

        // Initialize:
        coords1.x = 10;
        coords1.y = 20;

        // Display results:
        System.Console.Write("CoOrds 1: ");
        System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
    }
}
输出

CoOrds 1: x = 10, y = 20


---------------------------------------------------(本文转自网络、如有侵权、请告知!)

0 0
原创粉丝点击