Bài giảng Áp dụng cho các toán tử

Tài liệu Bài giảng Áp dụng cho các toán tử: Lập trỡnh trờn Windows với Microsoftđ .NET Giảng viờn : Hồ Hoàn Kiếm Overloading Methods Áp dụng cho cỏc toỏn tử : Overloading Methods public class Point { public int m_x; public int m_y; public Point (){ } public Point(int xx,int yy) { m_x = xx ; m_y = yy; } public static Point operator + (Point p1,Point p2) { Point result = new Point(); result.m_x = p1.m_x + p2.m_y; result.m_y = p1.m_x + p2.m_y; return result; } } Overloading Methods static void Main(string[] args) { Point objP1 = new Point(1,1); Point objP2 = new Point(2,2); Point objResult = new Point(); objResult = objP1 + objP2; Console.WriteLine("The result is m_x = {0} and m_y = {1}", objResult.m_x , objResult.m_y); } Kiểu Structs Kiểu dữ liệu do người dựng định nghĩa. Cú thể định nghĩa cỏc phương thức. Cú thể định nghĩa cỏc Constructors Structs là kiểu tham trị . Structs khụng được kế thừa . Kiểu Structs public struct Point { public int x, y; public Point(int p1, int p2) { x = p1; y = p2; } ...

ppt20 trang | Chia sẻ: hunglv | Lượt xem: 983 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Bài giảng Áp dụng cho các toán tử, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Lập trỡnh trờn Windows với Microsoftđ .NET Giảng viờn : Hồ Hoàn Kiếm Overloading Methods Áp dụng cho cỏc toỏn tử : Overloading Methods public class Point { public int m_x; public int m_y; public Point (){ } public Point(int xx,int yy) { m_x = xx ; m_y = yy; } public static Point operator + (Point p1,Point p2) { Point result = new Point(); result.m_x = p1.m_x + p2.m_y; result.m_y = p1.m_x + p2.m_y; return result; } } Overloading Methods static void Main(string[] args) { Point objP1 = new Point(1,1); Point objP2 = new Point(2,2); Point objResult = new Point(); objResult = objP1 + objP2; Console.WriteLine("The result is m_x = {0} and m_y = {1}", objResult.m_x , objResult.m_y); } Kiểu Structs Kiểu dữ liệu do người dựng định nghĩa. Cú thể định nghĩa cỏc phương thức. Cú thể định nghĩa cỏc Constructors Structs là kiểu tham trị . Structs khụng được kế thừa . Kiểu Structs public struct Point { public int x, y; public Point(int p1, int p2) { x = p1; y = p2; } public int Add() { return x+y; } } Kiểu Structs static void Main(string[] args) { Point p1 = new Point() ; Point p2 = new Point(10,10); Console.Write(" Point 1: "); Console.WriteLine("x = {0}, y = {1}", p1.x, p1.y); Console.Write(" Point 2: "); Console.WriteLine("x = {0}, y = {1}", p2.x, p2.y); Console.WriteLine("Call Add method: {0}",p2.Add()); Console.ReadLine(); } Kiểu Enumrator Tập hợp cỏc hằng số đó được đặt tờn. enum Days { //mac dinh Phan tu dau tien bat tu 0 Sat, Sun, Mon, Tue, Wed, Thu, Fri }; Kiểu Enumrator public static void Main() { int x = (int) Days.Sun; int y = (int) Days.Fri; Console.WriteLine("Sun = {0} : Value : {1}", x,Days.Sun); Console.WriteLine("Fri = {0} : Value : {1}", y,Days.Fri ); } Kết quả : Sun = 2 : Value : Sun Fri = 7 : Value : Fri Properties Sử dụng để truy cập cỏc thành phần Private. Kiềm tra dữ liệu cho cỏc thành phần trong lớp . public class Square { public double mSide; } public class Test { static void Main() { Square obj = new Square (); obj. mSide = 3; } } Properties … public class Square { //Khi bỏo cỏc thành phần private double mSide; // Khai bỏo property public double Side { get { }; set { }; } // Khai bỏo cỏc phương thức } Properties … Property giống như field, nhưng việc truy cập đươc thực hiện qua thao tỏc get và set . public int Side { get { return mSide; } set { if (mSide Length = 3; m_Width = 2; } Properties … public int Length { get { return m_Length; } set { if(m_Length = 100) return 0; else return myArray[index]; } set { if (!(index = 100)) myArray[index] = value; } } } Indexer public class Test { public static void Main() { IndexerClass b = new IndexerClass(); b[3] = 256; b[5] = 1024; for (int i=0; i<=10; i++) { Console.WriteLine("Element # {0} = {1}", i, b[i]); } } }

Các file đính kèm theo tài liệu này:

  • pptC# co ban 4-HHK.ppt