C# Class Struct =

来源:互联网 发布:算法设计技巧与分析书 编辑:程序博客网 时间:2024/05/18 03:35
//class 是引用类型,structs是值类型
//struct = 直接拷贝一份数据
//class = 传递引用 
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    struct Vector3    {        public float x;        public float y;        public float z;        public Vector3(float x,float y,float z)        {            this.x = x;            this.y = y;            this.z = z;        }        public static Vector3 operator*(Vector3 v1, Vector3 v2)        {            return new Vector3(v1.x * v2.x,v1.y * v2.y , v1.z * v2.z);        }        public static Vector3 operator *(Vector3 v1, float value)        {            return new Vector3(v1.x * value, v1.y * value, v1.z * value);        }        public override string ToString()        {            return "(" + x.ToString() + "," + y.ToString() + "," + z.ToString() + ")";        }    }    class Program    {        static void Main(string[] args)        {            Vector3 v1 = new Vector3(1, 1, 1);            Vector3 v2 = v1;            Console.WriteLine(v1.ToString() + " " + (v2).ToString());            //v1 *= 2;            v1.x = 2;            v2.y = 2;            Console.WriteLine(v1.ToString() + " " + v2.ToString());        }    }}
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{    class Vector3    {        public float x;        public float y;        public float z;        public Vector3(float x,float y,float z)        {            this.x = x;            this.y = y;            this.z = z;        }        public static Vector3 operator*(Vector3 v1, Vector3 v2)        {            return new Vector3(v1.x * v2.x,v1.y * v2.y , v1.z * v2.z);        }        public static Vector3 operator *(Vector3 v1, float value)        {            return new Vector3(v1.x * value, v1.y * value, v1.z * value);        }        public override string ToString()        {            return "(" + x.ToString() + "," + y.ToString() + "," + z.ToString() + ")";        }    }    class Program    {        static void Main(string[] args)        {            Vector3 v1 = new Vector3(1, 1, 1);            Vector3 v2 = v1;            Console.WriteLine(v1.ToString() + " " + (v2).ToString());            //v1 *= 2;            v1.x = 2;            v2.y = 2;            Console.WriteLine(v1.ToString() + " " + v2.ToString());        }    }}