Bài giảng Lập trình trên Windows với Microsoft® .NET: Bài 4 - Hồ Hoàn Kiếm
Số trang: 20
Loại file: ppt
Dung lượng: 346.00 KB
Lượt xem: 8
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng Lập trình trên Windows với Microsoft® .NET: Bài 4 của Hồ Hoàn Kiếm sau đây sẽ giúp cho các bạn hiểu rõ hơn về Overloading Methods; kiểu Structs; Properties; Indexer. Mời các bạn tham khảo bài giảng để bổ sung thêm kiến thức về lĩnh vực này.
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình trên Windows với Microsoft® .NET: Bài 4 - Hồ Hoàn KiếmLậptrìnhtrênWindows vớiMicrosoft® .NET Giảng viên : Hồ Hoàn KiếmOverloading 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 : FriProperties 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 < 0) return ; mSide = value; } }Properties … namespace SampleProperty { public class Rectangle { private int m_Length ; private int m_Width ; private int m_Area ; public Rectangle() { m_Length = 3; // Length = 3; m_Width = 2; }Properties … public int Length { get { return m_Length; } set { if(m_Length < 0 ) return; m_length = value; } }Properties … public int Width { get { return m_Width; } set { // Đọan mã kiểm tra giá trị ….. m_Width = value; } }Properties … public int Area { get { return m_Area; } } public void CalArea() { m_Area = m_Length*m_Width; }Properties … public class Test { public static void Main() { Rectangle objRectangle = new Rectangle(); objRectangle. Length = 3; objRectangle. Width = 4; objRectangle.CalArea(); Console.WriteLine({0}, objRectangle. Area ); } }Indexer Khi thành phần của lớp là các kiểu tập hợp. Sử dụng với : new, virtual, override, abstract, extern. Indexer giống như Property, đuợc truy cập thông qu ...
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình trên Windows với Microsoft® .NET: Bài 4 - Hồ Hoàn KiếmLậptrìnhtrênWindows vớiMicrosoft® .NET Giảng viên : Hồ Hoàn KiếmOverloading 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 : FriProperties 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 < 0) return ; mSide = value; } }Properties … namespace SampleProperty { public class Rectangle { private int m_Length ; private int m_Width ; private int m_Area ; public Rectangle() { m_Length = 3; // Length = 3; m_Width = 2; }Properties … public int Length { get { return m_Length; } set { if(m_Length < 0 ) return; m_length = value; } }Properties … public int Width { get { return m_Width; } set { // Đọan mã kiểm tra giá trị ….. m_Width = value; } }Properties … public int Area { get { return m_Area; } } public void CalArea() { m_Area = m_Length*m_Width; }Properties … public class Test { public static void Main() { Rectangle objRectangle = new Rectangle(); objRectangle. Length = 3; objRectangle. Width = 4; objRectangle.CalArea(); Console.WriteLine({0}, objRectangle. Area ); } }Indexer Khi thành phần của lớp là các kiểu tập hợp. Sử dụng với : new, virtual, override, abstract, extern. Indexer giống như Property, đuợc truy cập thông qu ...
Tìm kiếm theo từ khóa liên quan:
Lập trình trên Windows với Microsoft® .NET Kỹ thuật lập trình Overloading Methods Toán tử trong lập trình Toán tử kiểu Structs Toán tử PropertiesTài liệu liên quan:
-
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 270 0 0 -
NGÂN HÀNG CÂU HỎI TRẮC NGHIỆM THIẾT KẾ WEB
8 trang 212 0 0 -
Giới thiệu môn học Ngôn ngữ lập trình C++
5 trang 198 0 0 -
Bài giảng Nhập môn về lập trình - Chương 1: Giới thiệu về máy tính và lập trình
30 trang 170 0 0 -
Luận văn: Nghiên cứu kỹ thuật giấu tin trong ảnh Gif
33 trang 154 0 0 -
Báo cáo thực tập Công nghệ thông tin: Lập trình game trên Unity
27 trang 120 0 0 -
Giáo trình về phân tích thiết kế hệ thống thông tin
113 trang 114 0 0 -
LUẬN VĂN: Tìm hiểu kỹ thuật tạo bóng cứng trong đồ họa 3D
41 trang 110 0 0 -
Bài giảng Kỹ thuật lập trình - Chương 10: Tổng kết môn học (Trường Đại học Bách khoa Hà Nội)
67 trang 107 0 0 -
Giáo trình Nhập môn lập trình VB6: Phần 2
184 trang 95 0 0