Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.

Similar presentations


Presentation on theme: "1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所."— Presentation transcript:

1 1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所

2 2 程式 DiceSimulation.Program (1/3) using System; namespace DiceSimulation { /* /* * 模擬擲骰子示範類別的宣告與物件的使用 * 模擬擲骰子示範類別的宣告與物件的使用 * 使用類別 Dice 的建構函式 * 使用類別 Dice 的建構函式 * 3/23/2008 * 3/23/2008 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) {

3 3 程式 DiceSimulation.Program (2/3) int[] count = new int[6]; // 累計點數出現次數 for (int i = 0; i < 6; ++i) for (int i = 0; i < 6; ++i) { count[i] = 0; count[i] = 0; } const int N = 12000; // 總擲骰次數 const int N = 12000; // 總擲骰次數 int seed = 123; int seed = 123; Dice dice = new Dice( seed ); Dice dice = new Dice( seed ); int faceValue; int faceValue; // 擲骰 N 次 // 擲骰 N 次 for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i) { faceValue = dice.FaceValue; faceValue = dice.FaceValue; ++count[faceValue-1]; ++count[faceValue-1]; dice.Toss(); dice.Toss(); }

4 4 程式 DiceSimulation.Program (3/3) // 印出結果 // 印出結果 for (int i = 0; i < 6; ++i) for (int i = 0; i < 6; ++i) { Console.WriteLine(" {0} appears {1} times ", i + 1, count[i]); Console.WriteLine(" {0} appears {1} times ", i + 1, count[i]); } } }}

5 5 程式 DiceSimulation.Dice (1/2) using System; namespace DiceSimulation { /* /* * 骰子 * 骰子 * 3/23/2008 * 3/23/2008 */ */ public class Dice public class Dice { int faceValue; int faceValue; Random rand; Random rand; public Dice() public Dice() { rand = new Random(); rand = new Random(); Toss(); Toss(); }

6 6 程式 DiceSimulation.Dice (2/2) public Dice(int seed) public Dice(int seed) { rand = new Random(seed); rand = new Random(seed); Toss(); Toss(); } public int FaceValue public int FaceValue { get { return faceValue; } get { return faceValue; } set { faceValue = value; } set { faceValue = value; } } public void Toss() public void Toss() { faceValue = rand.Next() % 6 + 1; faceValue = rand.Next() % 6 + 1; } }}

7 7 建構函式與解構函式 (Constructor and Destructor) 預設建構函式 (default constructor) 預設建構函式 (default constructor) 具參數之建構函式 具參數之建構函式 – 檢驗參數範圍 解構函式 解構函式

8 8 物件產生與消滅流程 static void main( string[] arg ) { Dice dice = new Dice( seed ); public Dice(int seed) {... } } ~Dice() {... } 刪除物件 dice 離開主函數, 程式結束 物件宣告 物件生成

9 9 存取修飾詞 (Access Modifiers) public public private private protected protected internal internal

10 10 練習 修改類別 Card ,改用建構函式設定初值, 以屬性取得 suit 及 faceValue 修改類別 Card ,改用建構函式設定初值, 以屬性取得 suit 及 faceValue Card suit : char Suit FaceValue faceValue : int

11 11 程式 UsingStatic.Program (1/2) using System; namespace UsingStatic { /* 示範靜態成員之使用 /* 示範靜態成員之使用 * 3/27/2007 * 3/27/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { Console.WriteLine( Console.WriteLine( " 請輸入要轉換的小時數與天數, 以一個空格分開 "); " 請輸入要轉換的小時數與天數, 以一個空格分開 "); string[] input = string[] input = (Console.ReadLine()).Split(' '); (Console.ReadLine()).Split(' '); int hours = int.Parse(input[0]); int hours = int.Parse(input[0]); int days = int.Parse(input[1]); int days = int.Parse(input[1]);

12 12 程式 UsingStatic.Program (2/2) int hoursToMins = int hoursToMins = TimeConversion.HoursToMins(hours); TimeConversion.HoursToMins(hours); int daysToHours = int daysToHours = TimeConversion.DaysToHours(days); TimeConversion.DaysToHours(days); Console.WriteLine(hours + " hours = " + Console.WriteLine(hours + " hours = " + hoursToMins + " minutes"); hoursToMins + " minutes"); Console.WriteLine(days + " days = " + Console.WriteLine(days + " days = " + daysToHours + " hours"); daysToHours + " hours"); Test t1 = new Test(); Test t1 = new Test(); Test t2 = new Test(); Test t2 = new Test(); Console.WriteLine(Test.GetNConstructed + Console.WriteLine(Test.GetNConstructed + " Test objects were constructed"); " Test objects were constructed"); Console.ReadLine(); Console.ReadLine(); } }}

13 13 程式 UsingStatic.TimeConversion (1/2) using System; namespace UsingStatic { /* /* * collection of time conversion routines * collection of time conversion routines * 3/27/2007 * 3/27/2007 */ */ public static class TimeConversion public static class TimeConversion { private const int HOURS_PER_DAY = 24; private const int HOURS_PER_DAY = 24; private const int MINS_PER_HOUR = 60; private const int MINS_PER_HOUR = 60;

14 14 程式 UsingStatic.TimeConversion (2/2) public static int HoursToMins( int hours ) public static int HoursToMins( int hours ) { return hours*MINS_PER_HOUR; } public static int DaysToHours( int days ) public static int DaysToHours( int days ) { return days*HOURS_PER_DAY; }}}

15 15 程式 UsingStatic.Test using System; namespace UsingStatic { public class Test public class Test { private static int nConstructed = 0; private static int nConstructed = 0; public Test() { public Test() { ++nConstructed; ++nConstructed; } public static int GetNConstructed { public static int GetNConstructed { get { get { return nConstructed; return nConstructed; } } }}

16 16 靜態成員與靜態類別 常數宣告 常數宣告 靜態成員應用場合 靜態成員應用場合 靜態函式 Main 靜態函式 Main 靜態類別應用場合 靜態類別應用場合

17 17 靜態成員的記憶配置 t1 記憶體地址 1 t2 記憶體地址 2 函式成員 Test 進入點 函式 Test() 進入地址 nConstructedTest 函式成員 GetNConstructed 進入點 記憶體地址

18 18 練習 (1/2) 在類別 Card 內增加靜態成員函式,累計產 生的 Card 物件數 在類別 Card 內增加靜態成員函式,累計產 生的 Card 物件數 寫一程式利用類別 Card 產生全副撲克牌, 放在陣列 deck 內 寫一程式利用類別 Card 產生全副撲克牌, 放在陣列 deck 內

19 19 練習 (2/2) 實作並測試一靜態類別 EqSolver ,內含兩 靜態成員函式 double Linear(double a, double b) 及 double Quadratic(double a, double b, double c) 分別解一次方程式 a x + b = 0 及 a x 2 + b x + c = 0 實作並測試一靜態類別 EqSolver ,內含兩 靜態成員函式 double Linear(double a, double b) 及 double Quadratic(double a, double b, double c) 分別解一次方程式 a x + b = 0 及 a x 2 + b x + c = 0

20 20 運算子多載 可以多載 可以多載 – 一元: + 、 - 、 ! 、 ~ 、 ++ 、 -- 、 true 、 false – 二元: + 、 - 、 * 、 / 、 % 、 & 、 | 、 ^ 、 > 、 == 、 != 、 > 、 = 、 > 、 == 、 != 、 > 、 = 、 <= 不可多載 不可多載 –= 、. 、 && 、 || 、 ?: 、 [] 、 () 、 -> 、 new 、 is 、 sizeof 、 typeof 、 += 、 -= 、 *= 、 /= 、 %= 、 &= 、 |= 、 ^= 、 >= 轉換運算子 轉換運算子 –explicit 關鍵字 –Implicit 關鍵字

21 21 UsingOperOverload.Program(1/2) using System; namespace UsingOperOverload { /* 示範一元運算子多載 /* 示範一元運算子多載 * 4/9/2007 * 4/9/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { Rectangle rec = new Rectangle(100, 50); Rectangle rec = new Rectangle(100, 50); Console.WriteLine(" 長方形 rec 的面積 : " + Console.WriteLine(" 長方形 rec 的面積 : " + rec.Area()); rec.Area());

22 22 UsingOperOverload.Program(2/2) rec++; Console.WriteLine(" 長方形 rec++ 後的面積 : " + Console.WriteLine(" 長方形 rec++ 後的面積 : " + rec.Area()); // 應為 5151 rec.Area()); // 應為 5151 ++rec; ++rec; Console.WriteLine(" 長方形 ++rec 後的面積 : " + Console.WriteLine(" 長方形 ++rec 後的面積 : " + rec.Area()); // 應為 5304 rec.Area()); // 應為 5304 Console.ReadLine(); Console.ReadLine(); } }}

23 23 UsingOperOverload.Rectangle(1/2) using System; namespace UsingOperOverload { public class Rectangle public class Rectangle { private int width; private int width; private int length; private int length; private int area; private int area; public Rectangle(int width, int length) public Rectangle(int width, int length) { this.width = width; this.width = width; this.length = length; this.length = length; area = width * length; area = width * length; }

24 24 UsingOperOverload.Rectangle(2/2) public int Area() public int Area() { return area; return area; } public static Rectangle operator ++(Rectangle op) public static Rectangle operator ++(Rectangle op) { Rectangle result = new Rectangle(op.width+1, Rectangle result = new Rectangle(op.width+1, op.length+1); op.length+1); return result; return result; } }}

25 25 UsingOperOverload2.Program(1/2) using System; namespace UsingOperOverload2 { /* /* * 示範二元運算子多載的應用 * 示範二元運算子多載的應用 * 4/9/2007 * 4/9/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { Rectangle rec1 = new Rectangle(100, 50); Rectangle rec1 = new Rectangle(100, 50); Rectangle rec2 = new Rectangle(90, 25); Rectangle rec2 = new Rectangle(90, 25); Rectangle recSum = rec1 + rec2; Rectangle recSum = rec1 + rec2; Rectangle recDif = rec1 - rec2; Rectangle recDif = rec1 - rec2;

26 26 UsingOperOverload2.Program(2/2) // recSum.Area() 應為 14250 // recSum.Area() 應為 14250 Console.WriteLine("rec1 + rec2 的面積 : " + Console.WriteLine("rec1 + rec2 的面積 : " + recSum.Area()); recSum.Area()); // recDif.Area() 應為 250 // recDif.Area() 應為 250 Console.WriteLine("rec1 - rec2 的面積 : " + Console.WriteLine("rec1 - rec2 的面積 : " + recDif.Area()); recDif.Area()); Console.ReadLine(); Console.ReadLine(); } }}

27 27 UsingOperOverload2.Rectangle(1/2) using System; namespace UsingOperOverload2 { class Rectangle class Rectangle { private int width; private int width; private int length; private int length; private int area; private int area; public Rectangle(int width, int length) public Rectangle(int width, int length) { this.width = width; this.width = width; this.length = length; this.length = length; area = width * length; area = width * length; }

28 28 UsingOperOverload2.Rectangle(2/2) public int Area() public int Area() { return area; } { return area; } public static Rectangle operator +(Rectangle op1, public static Rectangle operator +(Rectangle op1, Rectangle op2) { Rectangle result = new Rectangle( Rectangle result = new Rectangle( op1.width + op2.width, op1.length + op2.length); op1.width + op2.width, op1.length + op2.length); return result; return result; } public static Rectangle operator -(Rectangle op1, Rectangle op2){ public static Rectangle operator -(Rectangle op1, Rectangle op2){ Rectangle result = new Rectangle( Rectangle result = new Rectangle( op1.width - op2.width, op1.length - op2.length); op1.width - op2.width, op1.length - op2.length); return result; return result; } }}

29 29 運算子執行流程 Rectangle recSum = rec1 + rec2; public static Rectangle operator +(Rectangle op1, Rectangle op2) { return result; } Rectangle result = new Rectangle( op1.width + op2.width, op1.width + op2.width, op1.length + op2.length); op1.length + op2.length);

30 30 練習 撰寫測試主程式及類別 Rational ( 有理數 ) , 其中須測試及定義有理數 + 、 - 、 * 、 / 、 ++ 運算子。可不必考慮約分。 撰寫測試主程式及類別 Rational ( 有理數 ) , 其中須測試及定義有理數 + 、 - 、 * 、 / 、 ++ 運算子。可不必考慮約分。

31 31 RelOperOverload.Program(1/2) using System; namespace RelOperOverload { /* /* * 示範關聯運算子 > 和 和 < 的多載 * 4/11/2007 * 4/11/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { StudentClass clsA = new StudentClass(20); StudentClass clsA = new StudentClass(20); StudentClass clsB = new StudentClass(30); StudentClass clsB = new StudentClass(30); bool clsAIsLarger = clsA > clsB; bool clsAIsLarger = clsA > clsB; string message = clsAIsLarger ? string message = clsAIsLarger ? "A 班人數大於 B 班 " : "A 班人數不大於 B 班 "; "A 班人數大於 B 班 " : "A 班人數不大於 B 班 ";

32 32 RelOperOverload.Program(2/2) Console.WriteLine(message); Console.ReadLine(); Console.ReadLine(); } }}

33 33 RelOperOverload.StudentClass(1/3) using System; namespace RelOperOverload { public class StudentClass public class StudentClass { private int nStudents; private int nStudents; public StudentClass(int nStudents) public StudentClass(int nStudents) { if (nStudents < 0) if (nStudents < 0) { this.nStudents = 0; this.nStudents = 0; } else else { this.nStudents = nStudents; this.nStudents = nStudents; } }

34 34 RelOperOverload.StudentClass(2/3) public int accessNStudents public int accessNStudents { get get { return nStudents; return nStudents; } } public static bool operator >(StudentClass op1, public static bool operator >(StudentClass op1, StudentClass op2) StudentClass op2) { bool result = (op1.nStudents > op2.nStudents); bool result = (op1.nStudents > op2.nStudents); return result; return result; }

35 35 RelOperOverload.StudentClass(3/3) public static bool operator <(StudentClass op1, public static bool operator <(StudentClass op1, StudentClass op2) StudentClass op2) { bool result = (op1.nStudents < op2.nStudents); bool result = (op1.nStudents < op2.nStudents); return result; return result; } }}

36 36 練習 在類別 Rational ( 有理數 ) 中添加有理數 > 、 、 < 、 ! ( 檢驗是否為 0) 運算子,並予測試。 處理 Rational ( 有理數 ) 類別與整數進行 + 、 - 、 * 、 / 的情形 處理 Rational ( 有理數 ) 類別與整數進行 + 、 - 、 * 、 / 的情形

37 37 RelOperOverload2.Program(1/2) using System; namespace RelOperOverload2 { /* /* * 示範關聯運算子 ==,!=,Equals,GetHashCode 等的多載 * 示範關聯運算子 ==,!=,Equals,GetHashCode 等的多載 * 4/11/2007 * 4/11/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { StudentClass clsA = new StudentClass("A", 20); StudentClass clsA = new StudentClass("A", 20); StudentClass clsB = new StudentClass("B", 20); StudentClass clsB = new StudentClass("B", 20); bool clsAEqualsclsB = clsA == clsB; bool clsAEqualsclsB = clsA == clsB; string message = clsAEqualsclsB ? string message = clsAEqualsclsB ? “clsA 與 clsB 相等 ”: “clsA 與 clsB 不等 "; “clsA 與 clsB 相等 ”: “clsA 與 clsB 不等 ";

38 38 RelOperOverload2.Program(2/2) Console.WriteLine(message); clsAEqualsclsB = clsA.Equals(clsB); clsAEqualsclsB = clsA.Equals(clsB); message = clsAEqualsclsB ? “clsA 與 clsB 相等 ": message = clsAEqualsclsB ? “clsA 與 clsB 相等 ": “clsA 與 clsB 不等 "; “clsA 與 clsB 不等 "; Console.WriteLine(message); Console.WriteLine(message); Console.ReadLine(); Console.ReadLine(); } }}

39 39 RelOperOverload2.StudentClass(1/3) using System; namespace RelOperOverload2 { public class StudentClass public class StudentClass { private int nStudents; private int nStudents; private string name; private string name; public StudentClass(string name, int nStudents) public StudentClass(string name, int nStudents) { this.name = name; this.name = name; if (nStudents < 0) if (nStudents < 0) { this.nStudents = 0; this.nStudents = 0; }

40 40 RelOperOverload2.StudentClass(2/3) else else { this.nStudents = nStudents; this.nStudents = nStudents; } } public static bool operator ==(StudentClass op1, public static bool operator ==(StudentClass op1, StudentClass op2) { bool result = ( op1.name.Equals(op2.name) ) && bool result = ( op1.name.Equals(op2.name) ) && (op1.nStudents == op2.nStudents); (op1.nStudents == op2.nStudents); return result; return result; } public static bool operator !=(StudentClass op1, public static bool operator !=(StudentClass op1, StudentClass op2) { return !(op1 == op2); return !(op1 == op2); }

41 41 RelOperOverload2.StudentClass(3/3) public override bool Equals(object obj) public override bool Equals(object obj) { bool result = false; bool result = false; if (obj is StudentClass) { if (obj is StudentClass) { StudentClass op = (StudentClass) obj; StudentClass op = (StudentClass) obj; result = name.Equals(op.name) && result = name.Equals(op.name) && (nStudents == op.nStudents); (nStudents == op.nStudents); } return result; return result; } public override int GetHashCode() { public override int GetHashCode() { return nStudents.GetHashCode() + return nStudents.GetHashCode() + name.GetHashCode(); name.GetHashCode(); } }}

42 42 ConvertOper.Program (1/2) using System; namespace ConvertOper { /* /* * 示範 explicit 與 implicit 的轉換 * 示範 explicit 與 implicit 的轉換 * 4/11/2007 * 4/11/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { Rectangle rec = new Rectangle(15, 10); Rectangle rec = new Rectangle(15, 10); int area = (int) rec; int area = (int) rec; Console.WriteLine("rec 面積為 " + area); Square sq = new Square(20); Square sq = new Square(20);

43 43 ConvertOper.Program (2/2) rec = sq; rec = sq; area = (int)rec; area = (int)rec; Console.WriteLine("sq 面積為 " + area); Console.ReadLine(); Console.ReadLine(); } }}

44 44 ConvertOper.Rectangle (1/2) using System; namespace ConverOper { public class Rectangle public class Rectangle { int width; int width; int length; int length; int area; int area; public Rectangle(int width, int length) public Rectangle(int width, int length) { this.width = width; this.width = width; this.length = length; this.length = length; area = length * width; area = length * width; }

45 45 ConvertOper.Rectangle (2/2) public static explicit operator int(Rectangle op) public static explicit operator int(Rectangle op) { return op.area; return op.area; } }}

46 46 ConvertOper.Square using System; namespace ConvertOper { public class Square { public class Square { private int width; private int width; public Square(int width) { public Square(int width) { this.width = width; this.width = width; } public static implicit operator public static implicit operator Rectangle( Square op ) { Rectangle( Square op ) { Rectangle rec = new Rectangle( op.width, Rectangle rec = new Rectangle( op.width, op.width ); op.width ); return rec; return rec; } }}

47 47 練習 在類別 Rational ( 有理數 ) 中添加有理數 == 、 != 運算子及 Equals 、 GetHashCode 方法,並予測試。 在類別 Rational ( 有理數 ) 中添加有理數 == 、 != 運算子及 Equals 、 GetHashCode 方法,並予測試。 在類別 Rational ( 有理數 ) 中添加轉為 double 的 explicit 運算子,及 int 轉為 Rational 的 implicit 運算子 在類別 Rational ( 有理數 ) 中添加轉為 double 的 explicit 運算子,及 int 轉為 Rational 的 implicit 運算子


Download ppt "1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所."

Similar presentations


Ads by Google