1 運算子多載 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.

Slides:



Advertisements
Similar presentations
資料蒐集的方法(三):實驗法(實驗設計) (第七章)
Advertisements

第二章 研究主題(研究題 目)與研究問題.
1 Chemical and Engineering Thermodynamics Chapter 2 Conservation of mass and energy Sandler.
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
第九章 運銷通路 授課老師 簡立賢. 授課大綱 運銷通路之涵意及其基本結構  何謂運銷通路  運銷通路的基本結構 影響農產品運銷通路選擇之因素  產品因素  市場因素  廠商因素  法規因素 運銷效率之判斷  通路中階段數目與運銷效率  通路競爭與運銷效率.
第二章 太陽能電池的基本原理 及其結構 2-1 太陽能電池的基本原理 2-2 太陽能電池的基本結構 2-3 太陽能電池的製作.
Advanced Chemical Engineering Thermodynamics
 課程網頁 :  講師姓名 : 張苑 ( ㄩㄢˋ ) 瑩  實驗室 : 資電館 734 室 
如何將數字變成可用之資訊 現代化資料處理與應用概念. 如何將數字變成可用之資訊 人最容易接受的訊息是圖像化資訊。 在一堆數字中,要進行比較分析,一般會使用表格形 式計算與分析。 所以一般我們會將數字依關聯性, 轉換成表格計算與分析。 此表格一般稱試算表或稱表格。 再將結果轉換為圖表,進行比較與分析。
Review of Chapter 3 - 已學過的 rules( 回顧 )- 朝陽科技大學 資訊管理系 李麗華 教授.
1 真理大學運輸管理學系 實務實習說明 目錄  實務實習類別  實務實習條例  校外實習單位  實務實習成績計算方式  校外實習甄選 / 自洽申請流程  附錄:相關表格.
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
Chapter 2 聯立線性方程式與矩陣 緒言 線性方程式組 (systems of linear equations) 出現 在多數線性模式 (linear model) 中。根據以往解 題的經驗,讀者們也許已發現方程式的解僅與 該方程式的係數有關,求解的過程也僅與係數 的運算有關,只要係數間的相關位置不改變,
資料庫名稱 中國期刊全文資料庫 (China Journal Full-text Database)
Variables & Hypotheses
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
例外處理. 內 容 大 綱 例外的產生 捕捉例外 丟出例外 例外的產生 (1/4) 一般常見的執行時期錯誤包括整數除法分母為 0(divide by zero) ,陣列索引值越限 (array index out of range) ,輸入輸出錯誤 (I/O error) 、檔案 不存在或無法存取.
真理大學航空運輸管理學系 實務實習說明. 實務實習部份 實務實習 校內實習 校外實習 實習時數必須在 300 小時 ( 含 ) 以上才承認 校內實習時數及實習成績。 二個寒假 各一個月 暑假兩個月.
Section 2.2 Correlation 相關係數. 散佈圖 1 散佈圖 2 散佈圖的盲點 兩座標軸的刻度不同,散佈圖的外觀呈 現的相聯性強度,會有不同的感受。 散佈圖 2 相聯性看起來比散佈圖 1 來得強。 以統計數字相關係數做為客觀標準。
第 18 章 名稱空間與例外處理 18-1 名稱空間 18-1 名稱空間 18-2 例外處理 18-2 例外處理.
資訊科技概論 第5章 系統程式.
液晶顯示控制器之研究 謝曜式 中華大學電機工程學系 Department of Electrical Engineering Chung-Hua University Hsinchu, Taiwan.
南投縣社區大學 Excel 實務應用入門 講師 : 林泉成
具備人臉追蹤與辨識功能的一個 智慧型數位監視系統 系統架構 在巡邏模式中 ,攝影機會左右來回巡視,並 利用動態膚色偵測得知是否有移動膚色物體, 若有移動的膚色物體則進入到追蹤模式,反之 則繼續巡視。
數位內容學院專業學程 說明會 廖文宏 6/8/2006. 緣由 經濟部工業局為鼓勵優秀學子積極投入數位內 容產業,並縮短業界對於應屆畢業學生的 職前訓練時間,因此委託數位內容學院與大學 院校合作,為即將畢業的大四或研究所學 生,規劃開設數位內容相關課程,讓學生能在 理論與實務兼具的課程中培養數位內容專.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
影像處理學習營 練習(四)製作影片 【下】 影像處理學習營 教師 吳清月. 製作影片 學習重點 一. 設定影片秒數。  加入音樂,與影片做結合。 三. 影片存檔方式之選擇  上傳到網路.
國立中央大學電機工程學系 99 學年度第 2 學期 助教會議 中央大學電機工程學系 工程認證 1.
第三部分:研究設計 ( 二): 研究工具的信效度 與研究效度 (第九章之第 306 頁 -308 頁;第四章)
最新計算機概論 第 5 章 系統程式. 5-1 系統程式的類型 作業系統 (OS) : 介於電腦硬體與 應用軟體之間的 程式,除了提供 執行應用軟體的 環境,還負責分 配系統資源。
台灣客家知識治理之研究 -- 以 行政院客委會為例 發表人: 彭安麗 南華大學公共行政與政策研究所助理教授 發表人: 彭安麗 南華大學公共行政與政策研究所助理教授
Hung-Hsiang WuWindows Processing Design1 Chapter 3 基本觀念 變數宣告與型態 特殊運算子符號 字串與數值的轉換 類別與物件的觀念 建立新的專案 WinMain 程式進入點 Include Header File.
3.1 矩陣的行列式 3.2 使用基本運算求行列式 3.3 行列式的性質 3.4 特徵值介紹 3.5 行列式的應用
行政院國家科學委員會工程技術發展處自動化學門 * 試以國立成功大學製造工程研究所 鄭芳田教授 產學合作計畫 : 智慧預測保養系統之設計與實作 成果報告盤點為範例 國科會工程處專題計畫成果典藏 自動化學門成果報告盤點範例.
大華技術學院九十三學年度 資工系計算機概論教學大綱 吳弘翔. Wu Hung-Hsiang2 科目名稱:計算機概論與實習 適用班別:夜資工技一A 授課老師:吳弘翔 學分數: 4 修別:必修 老師信箱:
North Point Government Primary PM School 北角官立下午小學 應用 ‘ 基本能力學生評估 ’ 及 ‘ 網上學與教支援系統 ’ 經驗分享.
鄭瑞興的個人簡介 中山資工所 鄭瑞興.
教材名稱:網際網路安全之技術及其應用 (編號: 41 ) 計畫主持人:胡毓忠 副教授 聯絡電話: 教材網址: 執行單位: 政治大學資訊科學系.
The application of boundary element evaluation on a silencer in the presence of a linear temperature gradient Boundary Element Method 期末報告 指導老師:陳正宗終身特聘教授.
歡迎新生入學. 選 課 說 明 95 學年第一學期 辦理科目抵免時間: 95 年 9 月 4 日至 95 年 9 月 7 日 加退選時間: 95 年 9 月 13 日至 9 月 25 日 本畫面僅供學生參考,若有更新皆以學校畫面 為準,請學生即早試試 本畫面僅供學生參考,若有更新皆以學校畫面 為準,請學生即早試試.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
CHAPTER 9 電腦 林麗娟‧教學媒體與操作. The Role of Computer for Learning  區別電腦輔助教學 (Computer –Assisted Instruction, CAI) 與電腦管理教學 (Computer – Managed Instruction,
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
Chapter 3 Entropy : An Additional Balance Equation
大華技術學院九十五學年度 資工系計算機概論教學大綱 吳弘翔. Wu Hung-Hsiang2 科目名稱:計算機概論與實習 授課老師:吳弘翔 學分數: 4 修別:必修 老師信箱:
Chapter 10 m-way 搜尋樹與B-Tree
網路介紹及其運用 講師陳炯勳. 5-2 IP 協定 ( 一 ) IP 協定運作 (1) – 網路成員:主機 (Host) 與路由器 (Router) – 路由表 – 電報傳輸運作.
SQL 進階查詢.
國立清華大學資訊工程學系 資訊工程系 2009/11/03P-1 Quiz & Solution 09810CS_ Computer Systems & Application Fall.
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 壹 企業研究導論.
-Artificial Neural Network- Matlab操作介紹 -以類神經網路BPN Model為例
Visual C++重點複習.
資料結構實習-六.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
電機工程系 選課輔導說明會. 選課輔導說明會宗旨 電機系自 95 年開始,同步進行『工程教育 認證』以及『系科本位課程』兩大與學生 課程相關的認證計畫。 電機系自 95 年開始,同步進行『工程教育 認證』以及『系科本位課程』兩大與學生 課程相關的認證計畫。 本系每年至少一次的系教育諮詢委員會, 廣邀產業界、學界、家長、畢業生、在校.
閱選訂購 Approval Plan. 什麼是閱選訂購 ? 由圖書館與其所選定代理商簽 訂合約,代理商根據圖書館所 制定的選書興趣檔 (profile) 選 擇適合的圖書送至圖書館,由 圖書專員審核挑選過後才予以 購買,不合則主動退書。
資料探索在醫學資料庫之應用 組長:資四 A H 王志晟 組員:資四 A H 盧增錄 資四 A H 黃巧菱 資四 A H 王冠博 資四 A H 陳亭瑋.
Set Theory Sets 許多數學上的討論中(例如: algebra 、 analysis 、 geometric 等等) 經常藉助集合論中的符號或術語來說 明。集合論是十九世紀後期由 Boole ( 布爾; 1815~1864) 及 Cantor ( 康托爾; 1845 ~ 1918) 所發展出來的。
Mapping - 1 Mapping From ER Model to Relational DB.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
第 1 章 PC 的基本構造. 本章提要 PC 系統簡介 80x86 系列 CPU 及其暫存器群 記憶體: Memory 80x86 的分節式記憶體管理 80x86 的 I/O 結構 學習組合語言的基本工具.
VHDL語法(3).
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
RFID 認證 1/99 建國科技大學資管系 饒瑞佶 RFID 認證 CompTIA RFID + ( 國際 ) EPC/RFID ( 台灣 ) RFID 工程師教育認證 ( 台灣 ) 2/99.
1 抽象類別與介面 (Abstract Classes and Interfaces) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.
1 類別與物件 II (Classes and Objects II) 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所.
Presentation transcript:

1 運算子多載 鄭士康國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所

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

3 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());

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

5 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; }

6 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; } }}

7 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;

8 UsingOperOverload2.Program(2/2) // recSum.Area() 應為 // recSum.Area() 應為 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(); } }}

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

10 UsingOperOverload3.Program(1/2) using System; namespace UsingOperOverload3 { /* /* * 示範不同型別運算元之二元運算子多載 * 示範不同型別運算元之二元運算子多載 * 4/9/2007 * 4/9/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { StudentClass cls = new StudentClass(20); StudentClass cls = new StudentClass(20); StudentClass clsAfterAdd = cls + 10; StudentClass clsAfterAdd = cls + 10; StudentClass clsAfterSub = cls - 3; StudentClass clsAfterSub = cls - 3; Console.WriteLine(" 加選後班級人數 : " + Console.WriteLine(" 加選後班級人數 : " + clsAfterAdd.accessNStudents); clsAfterAdd.accessNStudents);

11 UsingOperOverload3.Program(2/2) Console.WriteLine(" 退選後班級人數 : " + clsAfterSub.accessNStudents); clsAfterSub.accessNStudents); Console.ReadLine(); Console.ReadLine(); } }}

12 UsingOperOverload3.StudentClass (1/3) using System; namespace UsingOperOverload3 { 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; } }

13 UsingOperOverload3.StudentClass (2/3) public int accessNStudents public int accessNStudents { get get { return nStudents; return nStudents; } } public static StudentClass operator public static StudentClass operator +(StudentClass op1, int op2) +(StudentClass op1, int op2) { StudentClass result = new StudentClass result = new StudentClass(op1.nStudents + op2); StudentClass(op1.nStudents + op2); return result; return result; }

14 UsingOperOverload3.StudentClass (3/3) public static StudentClass operator – public static StudentClass operator – (StudentClass op1, int op2) (StudentClass op1, int op2) { StudentClass result = new StudentClass result = new StudentClass(op1.nStudents - op2); StudentClass(op1.nStudents - op2); return result; return result; } }}

15 LogOperOverload.Program(1/2) using System; namespace LogOperOverload { /* /* * 示範邏輯運算子之多載 * 示範邏輯運算子之多載 * 4/10/2007 * 4/10/2007 */ */ class Program class Program { static void Main(string[] args) static void Main(string[] args) { StudentClass clsA = new StudentClass(30); StudentClass clsA = new StudentClass(30); StudentClass clsB = new StudentClass(40); StudentClass clsB = new StudentClass(40); StudentClass clsC = new StudentClass(0); StudentClass clsC = new StudentClass(0); bool canMerge = clsA & clsB; bool canMerge = clsA & clsB;

16 LogOperOverload.Program(2/2) string message = canMerge ? "A 班與 B 班可合併 " : string message = canMerge ? "A 班與 B 班可合併 " : "A 班與 B 班不可合併 "; "A 班與 B 班不可合併 "; Console.WriteLine(message); Console.WriteLine(message); bool canNotCancel = !clsC; bool canNotCancel = !clsC; message = canNotCancel ? "C 班不可裁撤 " : message = canNotCancel ? "C 班不可裁撤 " : "C 班可裁撤 "; "C 班可裁撤 "; Console.WriteLine(message); Console.WriteLine(message); Console.ReadLine(); Console.ReadLine(); } }}

17 LogOperOverload.StudentClass(1/3) using System; namespace LogOperOverload { 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; } }

18 LogOperOverload.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 < 50; bool result = op1.nStudents+op2.nStudents < 50; return result; return result; }

19 LogOperOverload.StudentClass(3/3) public static bool operator !(StudentClass op) public static bool operator !(StudentClass op) { bool result = op.nStudents > 0; bool result = op.nStudents > 0; return result; return result; } }}

20 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 班 ";

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

22 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; } }

23 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; }

24 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; } }}

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

26 RelOperOverload.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 不等 ";

27 RelOperOverload.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(); } }}

28 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; }

29 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); }

30 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(); } }}

31 ConverOper.Program (1/2) using System; namespace ConverOper { /* /* * 示範 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);

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

33 ConverOper.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; }

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

35 ConverOper.Square using System; namespace ConverOper { 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; } }}

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