1 / 108

C# 物件導向程式設計 for ASP.NET

C# 物件導向程式設計 for ASP.NET. 鄧姚文 http://www.ywdeng.idv.tw. 課程簡介. C# 的程式寫法 變數與型態 (Type & Abstract Data Type) 算式之運算符號 (Operator) 表示式 (expression) 陳述式 (statements) 副程式及函數 (Function/Method) 內建函數 (Inner Function) 例外處理. Visual Studio .Net 共用整合式開發環境. 方案 Solution 專案 Project

Télécharger la présentation

C# 物件導向程式設計 for ASP.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# 物件導向程式設計for ASP.NET 鄧姚文 http://www.ywdeng.idv.tw

  2. 課程簡介 C#的程式寫法 變數與型態 (Type & Abstract Data Type) 算式之運算符號 (Operator) 表示式(expression) 陳述式 (statements) 副程式及函數 (Function/Method) 內建函數 (Inner Function) 例外處理

  3. Visual Studio .Net 共用整合式開發環境 方案 Solution 專案 Project 一個方案之中可以包含多個專案

  4. 程式的編譯與執行 • 主控台應用程式 Console Application • 文字模式介面 • 視窗應用程式 • Windows Form • 網站 • Web Form

  5. 程式範例:SayHello • 請使用者輸入姓名並選擇性別 • 依照性別不同,向使用者打招呼時加上『先生』或『小姐』 • Console Application 版本 • 使用 Console.Read 和 Console.Write • Web 版本 • 使用 Label、 TextBox、 Button • 事件驅動模式 • IntelliSense 智慧型提示

  6. C# 基本語法 識別字(Identifier) 保留字(Keyword) 資料型別 變數宣告 型別轉換 運算子(Operator) 敘述與運算式

  7. C# 基本語法:識別字(Identifier) • 識別字(Identifier) • 必須以英文字母或底線開頭(中文字也可以) • 不得包含空白 • 最大長度不得超過1023個字元 • 大小寫字母視為不同 • 不得使用保留字 • 以多個英文字組成識別字,中間以底線分隔,或採用駱駝表示法 • 變數名稱的開頭建議以型別或類別簡寫表示 • 例如:txtName 、 lblPrompt

  8. C# 基本語法:保留字/關鍵字

  9. C# 基本語法:保留字/關鍵字 • 關鍵字是對編譯器有特殊意義而預先定義的保留識別項。 • 關鍵字必須具有一個前置的 @,才能做為程式中的識別項。 • 例如,@if 是有效的識別項,但是 if 則不是,因為 if 是一個關鍵字。

  10. 資料型別 整數型別(Integer) 浮點數型別(Floating Point) decimal 型別(錢) char 型別(字元) bool 型別(布林、真假值) object 型別(物件) string 型別(字串)

  11. 整數型別 kilo, mega, giga, tera, peta, exa, zetta

  12. 浮點數型別 float ratio = 1.5F

  13. Decimal 型別 相較於 double, decimal 有效位數較多,值域較小,適合金融運算。 decimal ratio = 32.25M

  14. Char 型別 字元採 Unicode 編碼 'A' 是一個字元 '文' 也是一個字元

  15. Bool 型別 只有 true 和 false 兩個值 bool thisOneIsBigger = thisOne > thatOne; if (thisOneIsBigger) { loadLargerOne(thisOne); } else { loadLargerOne(thatOne); }

  16. Object 型別 System.Object是一切類別的始祖

  17. String 型別 字串採 Unicode 編碼 字串並不僅僅是字元的線性集合 "This is a book" "這是第一行\n這是第二行\n目前路徑為C:\\Windows" 以 @ 括住的字串常值不處理逸出序列, 適用於Windows檔案名稱: string fileName = @"c:\data\20091122.txt";

  18. 數值資料 • 整數(Integer) • 十進位26,十六進位 0x1A,八進位032 • 浮點數(Floating Point) • 2300,2.3E3 • 字元(Character) • '文'、'a'、'\u0061'、'\x0061' • 字串(String) • "電腦" • 跳脫字元(Escape Characters) • '\n'

  19. 跳脫字元

  20. 逸出序列 (Escape Sequence)

  21. 變數宣告 int year = 2003, month = 2; char yuan = '元'; float salary = 31255.6F; string message = "恭賀新禧"; 變數要先宣告才能使用 不可使用未經初始化的變數 浮點數的預設型別是 double,欲指定為 float 必須於數字尾加 f 或 F

  22. 型別轉換 int b = 400; double x = b; int z = (int)x; • 隱含轉換 Implicit Conversion • 值域小的轉換成值域大的 • 強制轉換 Explicit Conversion • 值域大的轉換成值域小的 • 不同值域間互換

  23. 型別轉換:字串轉數字 • int n=int.Parse("1234"); • double r=double.Parse("3.14"); • 事實上是: • int n=System.Int32.Parse("1234"); • double r=System.Double.Parse("3.14"); • 物件轉成字串: • n.ToString();

  24. 常數符號宣告 const float PI = 3.1415F; const double EXCHANGE_RATE = 32.65; • 文字式 Literal • 3.1415 • "視窗標題" • 常數符號式 Symbolic • 全部用大寫英文字母 • 兩個英文字之間加底線

  25. 運算子 Operator 指定 Assignment Operator 算術 Arithmetic Operator 關係 Relational Operator 邏輯 Logical Operator 字串 String Operator 複合指定 位元運算 Bitwise Operator

  26. 運算子 Operator (cont'd) 運算子(Operator) 運算元(Operand) • 一元運算(Unary) • i++ • 二元運算(Binary) • x + y

  27. 指定運算子Assignment Operator sum = n1 + n2; sum = 0, count = 1, index = 100; 3 = x + y; 將等號右邊的值指定給等號右邊的變數

  28. 算術運算子Arithmetic Operator

  29. 關係運算子Relational Operator

  30. 邏輯運算子Logical Operator

  31. 字串運算子String Operator 關係運算子也適用於字串

  32. 複合指定

  33. 位元運算子Bitwise Operator AND & OR | XOR ^ 補數 ~ 右移 >> 左移 <<

  34. Bitwise Operator 練習一 AND Console.Write("判斷奇數/偶數\n請輸入一個整數:"); int n = Int32.Parse(Console.ReadLine()); const int MASK = 0x1; bool isEven = (n & MASK) == 0; if (isEven) { Console.WriteLine("{0} 是一個偶數", n); } else { Console.WriteLine("{0} 是一個奇數", n); } 判斷某一個整數是奇數還是偶數?

  35. Bitwise Operator 練習一 OR Console.Write("請輸入一個二進位數字:"); string line = Console.ReadLine(); int binary = Convert.ToInt32(line, 2); int mask = Convert.ToInt32("1000", 2); int result = binary | mask; Console.WriteLine("設定第四位元後:" + Convert.ToString(result, 2)); 將一個整數的第 4 個 bit 設成 1

  36. Bitwise Operator 練習一 AND 將一個整數的第 4 個 bit 設成 0

  37. 範例:計算三角形面積 • 三角形三邊長為 a, b, c • 其面積 A 為 • 試寫一程式讀入三角型的三邊長,輸出其面積 • 任意兩邊的和必須大於第三邊

  38. [accessmodifier] enumname [: integertype] { membername1 [= expression1], membername2 [= expression2], ... membernameN [= expressionN] } enum MyWeekDays : byte { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday } 列舉型別 enum

  39. 型別資訊運算子 (as、is、typeof、sizeof) • as 運算子的語法為「運算式/物件 as 型別」常用於物件型別降轉 • is 運算子的語法為「運算式/物件 is 型別」 • "abc" is string 會傳回 true,"abc" is int 傳回 false。 • typeof 運算子的語法為「typeof(型別)」 • typeof(string) 會傳回 System.String。 • sizeof 運算子的語法為「sizeof(實值型別)」 • sizeof(byte) 會傳回 1。

  40. 網頁表單 Web Form • 屬性 • BackColor 背景顏色 • Font 字型 • ForeColor 前景顏色 • Text 標題或內容文字 • Height/Width 控制項的高度/寬度 • Visible 控制項是否顯現 • Enabled 控制項是否啟用 • ReadOnly 控制項是否『不可編輯』?

  41. 屬性

  42. 網頁表單 Web Form 事件 • 事件 • Init 頁面初始化時 • Load 載入頁面時 • DataBinding 繫結資料時 • PreRender 頁面呈現前 • Click 被滑鼠左鍵按一下時 • TextChanged 內文改變時

  43. Label(標籤) • 顯示僅供讀取的文字 • 屬性 • (ID) 物件名稱 • Text 顯示的文字 • Font 字型 • BorderStyle 外框形狀

  44. TextBox 文字方塊 • 請使用者輸入資料 • 屬性 • Text 控制項中的文字 • PasswordChar 輸入密碼時顯示的文字(輸入之文字不顯示實際內容) • ReadOnly 唯讀 • TextMode 單行、多行、密碼輸入 • 多行則由 Rows 屬性設定行數

  45. Button 按鈕 • 傳送表單 • 屬性與事件 • Text 顯示文字 • Click 按一下

  46. 變數的命名

  47. 範例:登入畫面 • 設計一登入畫面 • 以一個 TextBox 作為帳號輸入 • 以一個 TextBox 作為密碼輸入 • 表單載入後輸入焦點自動切到帳號輸入 • 驗證規則:若輸入之帳號內容與密碼內容相同,則顯示"登入成功!",否則顯示"帳號或密碼錯誤!"

  48. 決策敘述 if {} if {} else {} switch case

More Related