320 likes | 428 Views
擴展類別. 鄭士康 國立台灣大學 電機工程學系 / 電信工程研究所 / 資訊網路與多媒體研究所. 程式 UsingConstructor.Program. using System; namespace UsingConstructor { class Program { static void Main(string[] args) { Adder adder1 = new Adder(); Console.WriteLine( "adder1.GetResult() = " +
E N D
擴展類別 鄭士康 國立台灣大學 電機工程學系/電信工程研究所/ 資訊網路與多媒體研究所
程式 UsingConstructor.Program using System; namespace UsingConstructor { class Program { static void Main(string[] args) { Adder adder1 = new Adder(); Console.WriteLine( "adder1.GetResult() = " + adder1.GetResult() ); Adder adder2 = new Adder(150, 250); Console.WriteLine("adder2.GetResult() = " + adder2.GetResult()); Console.ReadLine(); } } }
程式 UsingConstructor.Adder (1/2) using System; namespace UsingConstructor { /* 模擬加法器 * 3/26/2007 */ public class Adder { private int a = 0; private int b = 0; public Adder() { }
程式 UsingConstructor.Adder (2/2) public Adder(int a, int b) { this.a = a; this.b = b; } public int GetResult() { return a + b; } ~Adder() { } } }
建構式與解構式(Constructor and Destructor) • 預設建構式(default constructor) • 具參數之建構式 • 解構式
存取修飾詞(Access Modifiers) • public • private • protected • internal
程式 UsingIndexer.Program (1/2) using System; namespace UsingIndexer { /* 示範索引子之使用 * 3/26/2007 */ class Program { static void Main(string[] args) { int lBound = -2; int uBound = 2; MyArray myArray = new MyArray(lBound, uBound);
程式 UsingIndexer.Program (2/2) for (int i = lBound; i < uBound; i++) { myArray[i] = 2 * i; } Console.WriteLine(); for (int i = lBound; i < uBound; i++) { Console.WriteLine("myArray[{0}] = {1}", i, myArray[i]); } Console.WriteLine(); } } }
程式 UsingIndexer.MyArray (1/4) using System; namespace UsingIndexer { public class MyArray { int lBound; int uBound; int length; int[] array; public MyArray(int lBound, int uBound) { this.lBound = lBound; this.uBound = uBound; length = uBound - lBound;
程式 UsingIndexer.MyArray (2/4) if( length < 1 ) { Console.WriteLine( "MyArray索引上下限設定錯誤,陣列長度小於"); } else { array = new int[length]; } } public int this[int index] {
程式 UsingIndexer.MyArray (3/4) get { if (index >= uBound || index < lBound) { Console.WriteLine( "MyArray: 索引值{0}小於下限{1}或大於上限{2}", index, lBound, uBound); return 0; } else { return array[index - lBound]; } }
程式 UsingIndexer.MyArray (4/4) set { if (index >= uBound || index < lBound) { Console.WriteLine( "MyArray: 索引值{0}小於下限{1}或大於上限{2}", index, lBound, uBound); } else { array[index - lBound] = value; } } } } }
程式 Using2DIndexer.Program (1/2) using System; namespace Using2DIndexer { /* * 利用矩陣示範二為索引子用法 * 3/26/2007 */ class Program { static void Main(string[] args) { Matrix a = new Matrix(2, 3); a[1, 1] = 10; a[2, 1] = 11; a[1, 2] = 11;
程式 Using2DIndexer.Program (2/2) a[2, 2] = 12; a[1, 3] = 12; a[2, 3] = 13; for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { Console.Write(a[i, j] + "\t"); } Console.WriteLine(); } Console.ReadLine(); } } }
程式 Using2DIndexer.Matrix (1/3) using System; namespace Using2DIndexer { public class Matrix { int nRow; int nCol; int[,] array; public Matrix(int nRow, int nCol) { this.nRow = nRow; this.nCol = nCol; array = new int[nRow, nCol]; }
程式 Using2DIndexer.Matrix (2/3) public int this[int i, int j] { get { if (i < 1 || i > nRow || j < 1 || j > nCol) { Console.WriteLine("Matrix>>矩陣註標錯誤"); return 0; } else { return array[i - 1, j - 1]; } }
程式 Using2DIndexer.Matrix (3/3) set { if (i < 1 || i > nRow || j < 1 || j > nCol) { Console.WriteLine("Matrix>>矩陣註標錯誤"); } else { array[i - 1, j - 1] = value; } } } } }
練習 • 以矩陣類別改寫陣列那一章的矩陣相乘程式
程式 UsingStatic.Program (1/2) using System; namespace UsingStatic { /* 示範靜態成員之使用 * 3/27/2007 */ class Program { static void Main(string[] args) { Console.WriteLine( "請輸入要轉換的小時數與天數, 以一個空格分開"); string[] input = (Console.ReadLine()).Split(' '); int hours = int.Parse(input[0]); int days = int.Parse(input[1]);
程式 UsingStatic.Program (2/2) int hoursToMins = TimeConversion.HoursToMins(hours); int daysToHours = TimeConversion.DaysToHours(days); Console.WriteLine(hours + " hours = " + hoursToMins + " minutes"); Console.WriteLine(days + " days = " + daysToHours + " hours"); Test t1 = new Test(); Test t2 = new Test(); Console.WriteLine(Test.GetNConstructed + " Test objects were constructed"); Console.ReadLine(); } } }
程式 UsingStatic.TimeConversion (1/2) using System; namespace UsingStatic { /* * collection of time conversion routines * 3/27/2007 */ public static class TimeConversion { private const int HOURS_PER_DAY = 24; private const int MINS_PER_HOUR = 60;
程式 UsingStatic.TimeConversion (2/2) public static int HoursToMins( int hours ) { return hours*MINS_PER_HOUR; } public static int DaysToHours( int days ) { return days*HOURS_PER_DAY; } } }
程式 UsingStatic.Test using System; namespace UsingStatic { public class Test { private static int nConstructed = 0; public Test(){ ++nConstructed; } public static int GetNConstructed{ get{ return nConstructed; } } } }
靜態成員與靜態類別 • 靜態成員應用場合 • 靜態函式Main • 靜態類別應用場合
程式 UsingStruct.Program (1/2) using System; namespace UsingStruct { /* * 示範結構的使用 * 3/27/2007 */ class Program { static void Main(string[] args) { Student s1; s1.name = "趙大"; s1.regNo = "007";
程式 UsingStruct.Program (2/2) Console.WriteLine("姓名: " + s1.name + ", 學號: " + s1.regNo); Student s2 = new Student("錢二", "009"); Console.WriteLine("姓名: " + s2.name + ", 學號: " + s2.regNo); Console.ReadLine(); } } }
程式 UsingStruct.Student using System; namespace UsingStruct { public struct Student { public string name; public string regNo; public Student(string name, string regNo) { this.name = name; this.regNo = regNo; } } }
程式UsingNestedClass.Program (1/2) using System; namespace UsingNestedClass { /* * 示範巢狀類別的使用 * 3/27/2007 */ class Program { static void Main(string[] args) { OuterClass.NestedClass nC = new OuterClass.NestedClass(13579, "巢狀類別之字串"); Console.WriteLine("nC.number = " + nC.AccessNumber);
程式UsingNestedClass.Program (2/2) Console.WriteLine("nC.name = " + nC.AccessName); Console.WriteLine(); int outerNumber; string outerName; nC.GetOuterValues(out outerNumber, out outerName); Console.WriteLine("nC.outerNumber = " + outerNumber); Console.WriteLine("nC.outerName = " + outerName); Console.ReadLine(); } } }
程式UsingNestedClass.OuterClass (1/3) using System; namespace UsingNestedClass { public class OuterClass { private int outerNumber; private string outerName; public OuterClass(int outerNumber, string outerName) { this.outerNumber = outerNumber; this.outerName = outerName; }
程式UsingNestedClass.OuterClass (2/3) public class NestedClass { private int number; private string name; OuterClass outerObj; public NestedClass(int number, string name) { this.number = number; this.name = name; outerObj = new OuterClass(2468, "外部字串"); } public int AccessNumber{ get{ return number; } }
程式UsingNestedClass.OuterClass (3/3) public string AccessName { get { return name; } } public void GetOuterValues(out int outerNumber, out string outerName) { outerNumber = outerObj.outerNumber; outerName = outerObj.outerName; } } } }