Bài giảng Lập trình C# - Bài 8: Properties and Indexers

Khái niệm Properties Cú pháp viết properties Read only properties Write only properties Read and Write properties So sánh field (thuộc tính) và properties Khái niệm Indexers Cú pháp viết Indexers

ppt14 trang | Chia sẻ: candy98 | Lượt xem: 432 | Lượt tải: 0download
Bạn đang xem nội dung tài liệu Bài giảng Lập trình C# - Bài 8: Properties and Indexers, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
Bài 8 Properties and IndexersÔn tập lại bài cũLớp cơ sở trừu tượng(Abstract base class)Interface(giao diện)Multiple InterfaceExplicit InterfaceInterface InheritanceSo sánh abstract base class và InterfaceSubject name / Session# / 3 of TotalpageNội dung trình bàyKhái niệm PropertiesCú pháp viết propertiesRead only propertiesWrite only propertiesRead and Write propertiesSo sánh field (thuộc tính) và propertiesKhái niệm IndexersCú pháp viết IndexersProperties and IndexersKhái niệm PropertiesCác thuộc tính trong một lớp được truy xuất thông qua tên lớp hoặc đối tượng.Obj.studentID = Convert.toInt32(Console.ReadLine());Properties là thành phần của lớp cung cấp một cách thức để đọc và ghi giá trị cho các thuộc tính bên trong một lớpSử dụng properties linh họat họat hơn sử dụng trực tiếp field rất nhiều.Cú phápProperties and IndexersCú phápget accessor dùng để đọc giá trị của thuộc tínhset accessor dùng để gán giá trị cho thuộc tínhProperties and IndexersRead Only propertiesDùng để lấy giá trị của một thuộc tínhCú phápVí dụ DEMO read only propertiesProperties and IndexersWrite only propertiesDùng để gán giá trị cho một thuộc tínhCú phápVí dụ DEMO write only propertiesProperties and IndexersRead and Write propertiesCho phép gán và lấy giá trị của một thuộc tính bên trong lớpCú phápVí dụ DEMO read và Write propertiesProperties and IndexersSo sánh field(thuộc tính) và propertiesKhác nhau về cách khai báoField là các biến có thể sử dụng với ref và out trong khi properties thì khôngProperties có thể thực hiện các hành động để thay đổi giá trị của field, trong khi đó field thì không.Properties and IndexersIndexers là gì?Cho phép truy cập dữ liệu bên trong đối tượng giống như truy cập các phần tử trong một mảngProperties and IndexersCú pháp viết IndexersVí dụ DEMO indexersProperties and IndexersTham số của IndexersIndexers phải có ít nhất một tham sốTham số sẽ là chỉ số để truy cập đến dữ liệu bên trong đối tượng.Indexers có thể có nhiều tham số, ví dụ khi thành phần của lớp là mảng 2 chiều, khi đó indexers cần phải có 2 tham số để truy cập như mảng kiểu mảng 2 chiều.Trong một lớp có thể viết nhiều Indexers bằng cách overloadingVí dụ DEMO indexers overloadingProperties and IndexersTổng kếtKhái niệm PropertiesCú pháp viết propertiesRead only propertiesWrite only propertiesRead and Write propertiesSo sánh field (thuộc tính) và propertiesKhái niệm IndexersCú pháp viết IndexersProperties and Indexers