Properties and Indexers

Nội dung trình bày

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

 

 

 

 

ppt13 trang | Chia sẻ: NamTDH | Lượt xem: 1022 | Lượt tải: 0download
Nội dung tài liệu 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 Interface Explicit Interface Interface Inheritance So sánh abstract base class và Interface Subject name / Session# / * of Totalpage Nội dung trình bày 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 Properties and Indexers Khái niệm Properties Cá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ớp Sử dụng properties linh họat họat hơn sử dụng trực tiếp field rất nhiều. Cú pháp Properties and Indexers Cú pháp get accessor dùng để đọc giá trị của thuộc tính set accessor dùng để gán giá trị cho thuộc tính Properties and Indexers Read Only properties Dùng để lấy giá trị của một thuộc tính Cú pháp Ví dụ DEMO read only properties Properties and Indexers Write only properties Dùng để gán giá trị cho một thuộc tính Cú pháp Ví dụ DEMO write only properties Properties and Indexers Read and Write properties Cho phép gán và lấy giá trị của một thuộc tính bên trong lớp Cú pháp Ví dụ DEMO read và Write properties Properties and Indexers So sánh field(thuộc tính) và properties Khác nhau về cách khai báo Field là các biến có thể sử dụng với ref và out trong khi properties thì không Properties 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 Indexers Indexers 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ảng Properties and Indexers Cú pháp viết Indexers Ví dụ DEMO indexers Properties and Indexers Tham số của Indexers Indexers 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 overloading Ví dụ DEMO indexers overloading Properties and Indexers Tổng kết 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 Properties and Indexers

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

  • pptbai_08_4998.ppt
Tài liệu liên quan