zoukankan      html  css  js  c++  java
  • 如何选择使用结构或类

    struct是值类型,隐式继承自System.ValueType,struct实例被分配在堆栈上,由系统内存回收;class是引用类型,class的实例被分配在托管堆上,由GC回收。

    struct不可以有无参构造函数(这种说法不准确,ChuckLu 提醒应说成:不能包含显式的无参构造函数。也就是说,struct有默认的无参构造函数,不能显式声明),只能有带参构造函数;class当然可以有无参构造函数。

    struct和class都可以通过new关键字创建实例。

    struct不能用abstract修饰,可以在class前面加abstract关键字。

    struct作为参数传递时,传递的是值,而class传递的是引用地址。

    来看一个struct和class类型作为参数传递的例子。

        class Program
    
        {
    
            static void Main(string[] args)
    
            {
    
                PointStruct structPoint = new PointStruct();
    
                structPoint.x = 10;
    
                structPoint.y = 10;
    
                Console.WriteLine("struct的初始值是:x={0},y={1}",structPoint.x, structPoint.y);
    
                ChangeStructPoint(structPoint);
    
                Console.WriteLine("struct的初始值改变后是:x={0},y={1}", structPoint.x, structPoint.y);
    
                Console.WriteLine();
    
                PointClass pointClass = new PointClass(10, 10);
    
                Console.WriteLine("class的初始值是:x={0},y={1}", pointClass.x, pointClass.y);
    
                ChangeClassPoint(pointClass);
    
                Console.WriteLine("class的初始值改变后是:x={0},y={1}", pointClass.x, pointClass.y);
    
                Console.ReadKey();
    
            }
    
            static void ChangeStructPoint(PointStruct pointStruct)
    
            {
    
                pointStruct.x = 20;
    
                pointStruct.y = 20;
    
                Console.WriteLine("正在改变struct的值是:x={0},y={1}", pointStruct.x, pointStruct.y);
    
            }
    
            static void ChangeClassPoint(PointClass pointClass)
    
            {
    
                pointClass.x = 20;
    
                pointClass.y = 20;
    
                Console.WriteLine("正在改变class的值是:x={0},y={1}", pointClass.x, pointClass.y);
    
            }
    
        }
    
        public struct PointStruct
    
        {
    
            public int x;
    
            public int y;
    
            public PointStruct(int x, int y)
    
            {
    
                this.x = x;
    
                this.y = y;
    
            }
    
        }
    
        public class PointClass
    
        {
    
            public int x;
    
            public int y;
    
            public PointClass(int x, int y)
    
            {
    
                this.x = x;
    
                this.y = y;
    
            }
    
        }
    

    结果是:

    1

    以上, struct和class的结构相似,当struct实例作为方法参数传递时,在堆栈上拷贝了一份传递给方法,在方法内改变其值,改变的是另一份struct实例的值(ChangeStructPoint方法内部,按ChuckLu的理解,改变的是函数参数的值,对,就是这个意思);而class实例作为参数传递时,在堆栈上拷贝了一份传递给方法,堆栈上的2个实例指向同一个引用地址,改变托管堆上实例的值,也就相当于同时改变了堆栈上2个class实例的值。

    总结:class完全可以替代struct能实现的所有功能,但,对于一些相对小的数据结构,应该考虑使用struct。

  • 相关阅读:
    Taobao File System
    poj 1860 (Bellman_Ford判断正环)
    IOS研究院之打开照相机与本地相册选择图片
    mysql中timestamp,datetime,int类型的区别与优劣
    数学之路(3)-机器学习(3)-机器学习算法-贝叶斯定理(3)
    POJ 3740 DLX
    HDU4738【杭州网赛、判桥】
    umdh工具使用
    计算机组成与设计(八)—— 单周期处理器
    计算机组成与设计(七)—— 除法器
  • 原文地址:https://www.cnblogs.com/darrenji/p/4467966.html
Copyright © 2011-2022 走看看