5 static void Mainstring args int myint 1

  • Slides: 56
Download presentation

函式-課堂範例 程式功能 讓使用者輸入 5個數,將此五數相加後印出答案 程式內容 static void Main(string[ ] args) { int[ ] myint

函式-課堂範例 程式功能 讓使用者輸入 5個數,將此五數相加後印出答案 程式內容 static void Main(string[ ] args) { int[ ] myint ={1, 2, 3}; Console. Write. Line(A_add_B(myint)); Console. Read. Key(); } static int A_add_B(int[ ] a) { int sum=0; for (int i = 0; i < a. Length; i++) { sum += +a[i]; } return sum; }

函式-課堂範例 程式功能 讓程式暫停一下 程式內容 static void Main(string[ ] args) { Console. Write. Line(“hi 你好呀”);

函式-課堂範例 程式功能 讓程式暫停一下 程式內容 static void Main(string[ ] args) { Console. Write. Line(“hi 你好呀”); pause(); } static void pause() { Console. Read. Key(); }

函式 請輸入下列程式,看看執行結果 程式內容 static void Main(string[] args) { int b = 1; Console. Write.

函式 請輸入下列程式,看看執行結果 程式內容 static void Main(string[] args) { int b = 1; Console. Write. Line(b); test(b); Console. Write. Line(b); Console. Read. Key(); } static void test(int a) { a=4; }

函式 請輸入下列程式,看看執行結果 程式內容 static void Main(string[] args) { int[] b = {1, 2, 3};

函式 請輸入下列程式,看看執行結果 程式內容 static void Main(string[] args) { int[] b = {1, 2, 3}; Console. Write. Line(b[0]); test(b); Console. Write. Line(b[0]); Console. Read. Key(); } static void test(int[] a) { a[0]=4; }

方法多載(overloading)-課堂範例 程式功能 定義二數相加add 函式 分別傳入整數及浮點數進行計算 程式內容 static int add(int a, int b) { return

方法多載(overloading)-課堂範例 程式功能 定義二數相加add 函式 分別傳入整數及浮點數進行計算 程式內容 static int add(int a, int b) { return a + b; } static double add(double a, double b) { return a + b; } static void Main(string[] args) { Console. Write. Line(add(10 , 20)); Console. Write. Line(add(10. 5 , 20)); }

類別(class) 語法 存取修飾詞 class 類別名稱 { 類別成員; } 用法 public class car { public

類別(class) 語法 存取修飾詞 class 類別名稱 { 類別成員; } 用法 public class car { public string id; //class內含許多這樣的欄位 public void GO () //class內含許多方法(函式)method {} public void Stop() {} }

類別(class)-課堂範例 程式功能 創造一叫student的class 學生資料的欄位有ID , phone_number , math , chinese 有一個專屬的函式來計算學生的平均成績 程式內容 public class

類別(class)-課堂範例 程式功能 創造一叫student的class 學生資料的欄位有ID , phone_number , math , chinese 有一個專屬的函式來計算學生的平均成績 程式內容 public class student { public int ID; //有三個欄位 public int phone_number; public int math, chinese; public int average()//有一個method來算學生的平均成積 { return (math + chinese)/2; } }

類別(class)-課堂範例 static void Main(string[] args) { student john=new student(); john. chinese = 100; john.

類別(class)-課堂範例 static void Main(string[] args) { student john=new student(); john. chinese = 100; john. math = 90; int average; average=john. average(); Console. Write. Line("john's avarage="+ average); Console. Read. Key(); }

類別(class)-課堂練習 static void Main(string[] args) { STACK mystack = new STACK(size); mystack. push(1); mystack.

類別(class)-課堂練習 static void Main(string[] args) { STACK mystack = new STACK(size); mystack. push(1); mystack. push(2); mystack. push(4); mystack. push(3); mystack. push(6); Console. Write. Line( mystack. pop()); Console. Write. Line(mystack. pop()); }

類別(class)-建構子-課堂範例 實例 class car { private string ID; public car() { ID = "ABC-123";

類別(class)-建構子-課堂範例 實例 class car { private string ID; public car() { ID = "ABC-123"; } public car(string id) { ID = id; } public void show. Id() { Console. Write. Line("車牌號碼︰" + ID); } }

類別(class)-建構子-課堂範例 實例 static void Main(string[] args) { car my. Car 1 = new car();

類別(class)-建構子-課堂範例 實例 static void Main(string[] args) { car my. Car 1 = new car(); Console. Write("Car 1 "); my. Car 1. show. Id(); //將show出預設的ID “ABC-123” car my. Car 2 = new car("NTU-123"); Console. Write("Car 2 "); my. Car 2. show. Id(); //將show出使用者傳入的ID” NTU-123” }

類別(class)-繼承-課堂範例 例子: class clock { public int hours , minute , second; //時 ,

類別(class)-繼承-課堂範例 例子: class clock { public int hours , minute , second; //時 , 分 , 秒 } class Water_Resist_Clock : clock { public int water_resist=100; //防水深度 } class Ringing_Clock : clock { public int volume=30; //音量 }

類別(class)-繼承-課堂範例 class Program { static void Main(string[] args) { Water_Resist_Clock awatch = new Water_Resist_Clock();

類別(class)-繼承-課堂範例 class Program { static void Main(string[] args) { Water_Resist_Clock awatch = new Water_Resist_Clock(); awatch. water_resist = 100; awatch. hours = 14; awatch. minute = 30; awatch. second= 35; } }

類別(class)-存取修飾子-課堂範例 範例 class Water_Resist_Clock : clock { private int water_resist=100; //防水深度 public void set_water_resist(int

類別(class)-存取修飾子-課堂範例 範例 class Water_Resist_Clock : clock { private int water_resist=100; //防水深度 public void set_water_resist(int setting) { water_resist=setting; // 不會發生錯誤,因為private欄位在類別本體中可以使用 } } class Program { static void Main(string[] args) { Water_Resist_Clock awatch = new Water_Resist_Clock(); awatch. water_resist = 100; // 錯誤,因為是private的欄位外界無法使用 awatch. hours = 14; awatch. minute = 30; awatch. second= 35; } }

類別(class) -存取修飾子-課堂練習 例子: class clock { public static int tick=1; // 對所有時鐘來講tick都是一秒 public int

類別(class) -存取修飾子-課堂練習 例子: class clock { public static int tick=1; // 對所有時鐘來講tick都是一秒 public int hours , minute , second; // 個別的時鐘自有的時間 } class Program { static void Main(string[] args) { clock. tick=2; // 將所有時鐘的tick都改成兩秒 clock. hours=14; // 錯誤,沒有宣告任何clock物件的話,是不能使用hours 的 } }

類別(class)- 命名空間 例子-創造命名空間 namespace ASUS { class Notebook {…. } class seller {…. }

類別(class)- 命名空間 例子-創造命名空間 namespace ASUS { class Notebook {…. } class seller {…. } } namespace Compaq { class Notebook {…. } class seller {…. } }