国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET基礎知識:類和結構的區別是什么?

2024-07-10 13:13:10
字體:
來源:轉載
供稿:網友

類:
類是引用類型在堆上分配,類的實例進行賦值只是復制了引用,都指向同一段實際對象分配的內存
類有構造和析構函數
類可以繼承和被繼承
結構:
結構是值類型在棧上分配(雖然棧的訪問速度比較堆要快,但棧的資源有限放),結構的賦值將分配產生一個新的對象。
結構沒有構造函數,但可以添加。結構沒有析構函數
結構不可以繼承自另一個結構或被繼承,但和類一樣可以繼承自接口

示例:
根據以上比較,我們可以得出一些輕量級的對象最好使用結構,但數據量大或有復雜處理邏輯對象最好使用類。
如:geoemtry(gis 里的一個概論,在 ogc 標準里有定義) 最好使用類,而 geometry 中點的成員最好使用結構

using system;
using system.collections.generic;
using system.text;
namespace example16
{
    interface ipoint
    {
        double x
        {
            get;
            set;
        }
        double y
        {
            get;
            set;
        }
        double z
        {
            get;
            set;
        }
    }
    //結構也可以從接口繼承
    struct point: ipoint
    {
        private double x, y, z;
        //結構也可以增加構造函數
        public point(double x, double y, double z)
        {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public double x
        {
            get { return x; }
            set { x = value; }
        }
        public double y
        {
            get { return x; }
            set { x = value; }
        }
        public double z
        {
            get { return x; }
            set { x = value; }
        }
    }
    //在此簡化了點狀geometry的設計,實際產品中還包含project(坐標變換)等復雜操作
    class pointgeometry
    {
        private point value;
       
        public pointgeometry(double x, double y, double z)
        {
            value = new point(x, y, z);
        }
        public pointgeometry(point value)
        {
            //結構的賦值將分配新的內存
            this.value = value;
        }
        public double x
        {
            get { return value.x; }
            set { this.value.x = value; }
        }
        public double y
        {
            get { return value.y; }
            set { this.value.y = value; }
        }
        public double z
      {
            get { return value.z; }
            set { this.value.z = value; }
        }
        public static pointgeometry operator +(pointgeometry left, pointgeometry rigth)
        {
            return new pointgeometry(left.x + rigth.x, left.y + rigth.y, left.z + rigth.z);
        }
        public override string tostring()
        {
            return string.format("x: {0}, y: {1}, z: {2}", value.x, value.y, value.z);
        }
    }
    class program
    {
        static void main(string[] args)
        {
            point tmppoint = new point(1, 2, 3);
            pointgeometry tmppg1 = new pointgeometry(tmppoint);
            pointgeometry tmppg2 = new pointgeometry(tmppoint);
            tmppg2.x = 4;
            tmppg2.y = 5;
            tmppg2.z = 6;
            //由于結構是值類型,tmppg1 和 tmppg2 的坐標并不一樣
            console.writeline(tmppg1);
            console.writeline(tmppg2);
            //由于類是引用類型,對tmppg1坐標修改后影響到了tmppg3
            pointgeometry tmppg3 = tmppg1;
            tmppg1.x = 7;
            tmppg1.y = 8;
            tmppg1.z = 9;
            console.writeline(tmppg1);
            console.writeline(tmppg3);
            console.readline();
        }
    }
}


結果:
x: 1, y: 2, z: 3
x: 4, y: 5, z: 6
x: 7, y: 8, z: 9
x: 7, y: 8, z: 9

 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 拉萨市| 古丈县| 苗栗县| 东丰县| 瑞丽市| 察雅县| 德清县| 康马县| 海淀区| 博客| 综艺| 隆安县| 惠安县| 张家口市| 镶黄旗| 容城县| 米易县| 福建省| 喀喇| 广安市| 漳浦县| 光山县| 琼中| 吉水县| 麻江县| 汪清县| 镇赉县| 洪江市| 平顶山市| 临汾市| 化隆| 尉氏县| 临沂市| 菏泽市| 杭锦后旗| 嵩明县| 巴塘县| 育儿| 贡嘎县| 勐海县| 永安市|