240 likes | 409 Views
結構體與列舉型態. 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所. 程式 UsingStruct (1/2). using System; namespace UsingStruct { /* * 利用二維空間座標點說明結構的使用 * skj 4/26/2008 */ class Program { struct Point2D { public int x; public int y; }.
E N D
結構體與列舉型態 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
程式UsingStruct (1/2) using System; namespace UsingStruct { /* * 利用二維空間座標點說明結構的使用 * skj 4/26/2008 */ class Program { struct Point2D { public int x; public int y; }
程式UsingStruct (2/2) static void Main(string[] args) { Point2D pt = new Point2D(); Console.WriteLine( "Initial location = ({0},{1})", pt.x, pt.y); pt.x = 3; pt.y = 4; Console.WriteLine( "Final location = ({0},{1})", pt.x, pt.y); } } }
程式StructVSClass (1/6) using System; namespace StructVSClass { /* * 利用類別與結構分別宣告二維座標點 * 觀察其儲存方式之不同 * skj 4/26/2008 */ class Program {
程式StructVSClass (2/6) struct SPoint2D { public int x; public int y; public SPoint2D(int x, int y) { this.x = x; this.y = y; } }
程式StructVSClass (3/6) class CPoint2D { public int x; public int y; public CPoint2D() { x = 0; y = 0; } public CPoint2D(int x, int y) { this.x = x; this.y = y; } }
程式StructVSClass (4/6) static void Main(string[] args) { SPoint2D sPt1 = new SPoint2D(3, 4); SPoint2D sPt2 = new SPoint2D(); SPoint2D sPt3 = new SPoint2D(); sPt2 = sPt1; sPt3 = sPt1; Console.WriteLine("sPt1 = ({0}, {1})", sPt1.x, sPt1.y); Console.WriteLine("sPt2 = ({0}, {1})", sPt2.x, sPt2.y); Console.WriteLine("sPt3 = ({0}, {1})", sPt3.x, sPt3.y); CPoint2D cPt1 = new CPoint2D(3, 4); CPoint2D cPt2 = new CPoint2D(); CPoint2D cPt3 = new CPoint2D();
程式StructVSClass (5/6) cPt2 = cPt1; cPt3 = cPt1; Console.WriteLine("cPt1 = ({0}, {1})", cPt1.x, cPt1.y); Console.WriteLine("cPt2 = ({0}, {1})", cPt2.x, cPt2.y); Console.WriteLine("cPt3 = ({0}, {1})", cPt3.x, cPt3.y); sPt1.x = 10; sPt1.y = 20; sPt2.x = 30; sPt2.y = 40;
程式StructVSClass (6/6) Console.WriteLine("sPt1 = ({0}, {1})", sPt1.x, sPt1.y); Console.WriteLine("sPt2 = ({0}, {1})", sPt2.x, sPt2.y); Console.WriteLine("sPt3 = ({0}, {1})", sPt3.x, sPt3.y); cPt1.x = 10; cPt1.y = 20; cPt2.x = 30; cPt2.y = 40; Console.WriteLine("cPt1 = ({0}, {1})", cPt1.x, cPt1.y); Console.WriteLine("cPt2 = ({0}, {1})", cPt2.x, cPt2.y); Console.WriteLine("cPt3 = ({0}, {1})", cPt3.x, cPt3.y); }}}
結構 • 定義方式與類別相似 • 記憶配置於堆疊 • 適合使用於小型資料集合 • 減少記憶回收之負擔 • 不可自訂預設建構函式 • 成員變數不能直接設定初值 • 不支援繼承功能
類別物件記憶配置 cPt1 cPt1.x 記憶體地址1 cPt1.y cPt2 cPt2 = cPt1; cPt2.x 記憶體地址2 cPt2.y heap space
結構物件記憶配置 sPt1.x sPt1 sPt1.y sPt2 sPt2.x sPt2.y stack
練習 • 宣告並測試結構Student,其中包括學號、姓名、成績
程式UsingCopyConstructor (1/4) using System; namespace UsingCopyConstructor { /* * 示範複製建構式的使用 * skj 5/16/2008 */ class Program { class CPoint2D { public int x; public int y;
程式UsingCopyConstructor (2/4) public CPoint2D() { x = 0; y = 0; } public CPoint2D(int x, int y) { this.x = x; this.y = y; } public CPoint2D(CPoint2D p) { x = p.x; y = p.y; } }
程式UsingCopyConstructor (3/4) static void Main(string[] args) { CPoint2D cPt1 = new CPoint2D(3, 4); CPoint2D cPt2 = new CPoint2D( cPt1 ); CPoint2D cPt3 = new CPoint2D( cPt1 ); Console.WriteLine("cPt1 = ({0}, {1})", cPt1.x, cPt1.y); Console.WriteLine("cPt2 = ({0}, {1})", cPt2.x, cPt2.y); Console.WriteLine("cPt3 = ({0}, {1})", cPt3.x, cPt3.y); cPt1.x = 10; cPt1.y = 20; cPt2.x = 30; cPt2.y = 40;
程式UsingCopyConstructor (4/4) Console.WriteLine("cPt1 = ({0}, {1})", cPt1.x, cPt1.y); Console.WriteLine("cPt2 = ({0}, {1})", cPt2.x, cPt2.y); Console.WriteLine("cPt3 = ({0}, {1})", cPt3.x, cPt3.y); } } }
淺層複製與深層複製 • 淺層複製(Shallow copy) • 系統提供 • 只複製參考(Reference)地址 • 沒有新物件產生 • 深層複製(Deep copy) • 程式師提供 • 產生新物件 • 應複製所有資料成員
練習 • 宣告並測試類別Student,其中包括學號、姓名、成績,仿照程式StructVSClass及UsingCopyConstructor分別使用設值與複製建構函式產生Student物件,觀察淺層複製與深層複製的差別
程式 UsingEnum (1/3) using System; class UsingEnum { enum WeekDay { SUN = 1, MON = 2, TUE = 3, WED = 4, THU = 5, FRI = 6, SAT = 7 }
程式 UsingEnum (2/3) static void Main(string[] args) { WeekDay day = WeekDay.TUE; switch (day) { case WeekDay.SUN: Console.WriteLine("星期日為一週的第{0}天!!", (int)WeekDay.SUN); break; case WeekDay.MON: Console.WriteLine("星期一為一週的第{0}天!!", (int)WeekDay.MON); break; case WeekDay.TUE: Console.WriteLine("星期二為一週的第{0}天!!", (int)WeekDay.TUE); break;
程式 UsingEnum (3/3) default: Console.WriteLine("一週的第{0}天!!", (int)day); break; } Console.ReadLine(); } }
列舉型別 (Enumeration) • 組織整數常數與程式維護 • 省略數值指定 • 應用列舉型別
練習 • 宣告並測試列舉型別Season,其中包括常數Spring、Summer、Fall、Winter