Danh mục

Bài giảng Lập trình hướng đối tượng: Chương 4 - ĐH Ngoại ngữ - Tin học

Số trang: 30      Loại file: pptx      Dung lượng: 136.84 KB      Lượt xem: 12      Lượt tải: 0    
tailieu_vip

Xem trước 3 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 hướng đối tượng - Chương 4: Properties" cung cấp cho người học các kiến thức: Lý do dùng properties, tạo properties, auto – Implemented properties, object Initializer. Mời các bạn cùng tham khảo nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Lập trình hướng đối tượng: Chương 4 - ĐH Ngoại ngữ - Tin họcClick to edit Master subtitle style CHƯƠNG 4: PROPERTIES Khoa Công nghệ thông tin Trường Đại học Ngoại ngữ - Tin học, TP.HCM 1 NỘI DUNG• Lý do dùng properties• Tạo properties• Auto – Implemented properties• Object Initializer 2Lý do dùng properties 3 Lý do dùng properties• Ví dụ: class Time { private int seconds; } – Nhu cầu: Bên ngoài lớp có thể truy cập giá trị seconds 4 Lý do dùng properties• Giải pháp “cổ điển”: Tạo method trả về giá trị second và method gán giá trị cho second class { Time private int seconds; public int GetSeconds() { return seconds; } public void SetSeconds(int seconds) { this.seconds = seconds; if (this.seconds < 0) this.seconds = 0; } } 5 Lý do dùng properties• Nhận xét – Nhu cầu dùng method get và set cực kỳ nhiều – Cách viết trên có một chút “cồng kềnh” class Program { public static void Main() { Time time = new Time(); int x = time.GetSeconds(); time.SetSeconds(90); } } 6 Lý do dùng properties• Một cách cực kỳ “thuận lợi” nhưng không class Time ai dùng { public int seconds; } class Program { public static void Main() { Time time = new Time(); int s = time.seconds; time.seconds = 90; } } 7Tạo properties 8 Tạo propertiesclass Time class Time{ { private int seconds; private int seconds; public int Seconds public int GetSeconds() { { get return seconds; { } return seconds; } public void SetSeconds(int set seconds) { { this.seconds = value; this.seconds = seconds; if (this.seconds < 0) if (this.seconds < 0) this.seconds = 0; this.seconds = 0; } } } }} 9 Tạo properties• Khai báo property: có 3 phần – Từ khóa ‘public’: Ai cũng có thể dùng property – Kiểu của property – Tên property• Thân của property – get: thực thi khi ai đó đọc giá trị của property – set: thực thi khi ai đó gán giá trị cho property 10 Tạo properties• Nội dung của get và set giống hàm GetSeconds và SetSeconds• Hàm SetSeconds có tham số còn set không có tham số mà dùng từ khóa value đại diện cho giá trị gán cho property 11 Tạo propertiesclass Program class Program{ { public static void Main() public static void Main() { { Time time = new Time(); Time time = new Time(); time.Seconds = 90; time.SetSeconds(90); } }} } 12 Tạo properties• Tóm tắt { get { … return …; } set { … } } 13 Tạo properties• Backing field – Property có get hay set giá trị cho field thì field đó gọi là backing field của property – Một property không nhất thiết có backing field 14 Tạo propertiesclass ...

Tài liệu được xem nhiều: