Bài giảng Lập trình trên Windows Chương 2 Trần Minh Thái (Phần 2)

96 329 1
Bài giảng Lập trình trên Windows Chương 2  Trần Minh Thái (Phần 2)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài giảng Lập trình trên Windows Chương 2 Ngôn ngữ lập trình C (phần 2) cung cấp cho người học các kiến thức Interface, property, Mảng và Indexer, lớp Collection. Mời các bạn cùng tham khảo nội dung chi tiết.

Lập trình Windows Chương Ngôn ngữ lập trình C# Phần Nội dung • Interface • Property, Mảng Indexer • Lớp Collection 22 Interface Interface Khái niệm Interface Khái niệm • Interface định nghĩa “giao kèo” mà thực lớp • Một interface chứa methods, properties, events, indexers • Interface không cung cấp thực thành viên định nghĩa • Interface lớp trừu tượng túy Interface Khai báo • Cú pháp [attributes][modifiers]interface[:baseList] {     [interface-body] } Interface Khai báo • Tên Interface • Đặt giống tên lớp • Ký tự “I” interface IControl { void Paint(); } Interface Khai báo • Danh sách thừa kế • Interface thừa kế từ interface khác • Interface thực đa thừa kế interface IControl { void Paint(); } interface ITextBox: IControl { void SetText(string text); } interface IListBox: IControl { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox {} Interface Khai báo • Các thành phần bên interface • Phương thức • Property • Indexer • Event • Và thành phần khác thừa kế từ interface • Nhận xét • Không có field interface • Không có constructor destructor • Không có kiểu lồng • Không viết access modifier cho thành viên Interface Hiện thực • Quy tắc • Một lớp thực hay nhiều interface • Khi lớp cài đặt interface phải cài đặt thành viên interface • Cách thực • Dùng thành viên public • Hoặc rõ thành viên thuộc interface (tránh tạo thành viên public) 10 Collections ArrayList/List • Thêm liệu listName.Add(data);  Truy cập liệu listName[index]; foreach (type variable in listName) { xử lý variable } 82 Collections ArrayList/List • Xóa liệu listName.Remove(data); listName.Clear();  Chèn liệu listName.Insert(index, data);  Sắp xếp listName.Sort(); 83 Collections ArrayList/List • Tìm kiếm liệu bool listName.Contains(data); int listName.IndexOf(data); int listName.LastIndexOf(data); 84 Collections ArrayList/List Methods Ý nghĩa int Add(object) Thêm đối tượng vào cuối mảng void Clear() Xóa bool Contains(object) Kiểm tra có tồn đối tượng mảng int IndexOf(object) Tìm kiếm từ trái sang phải int LastIndexOf(object) Tìm kiếm từ phải sang trái void Insert(index, object) Chèn đối tượng vị trí void Remove(object) Xóa phần từ void RemoveAt(index) Xóa phần tử vị trí void RemoveRange(index, count) Xóa số phần tử void Sort() Sắp xếp tăng dần int BinarySearch(object) Tìm kiếm nhị phân void Reverse() Đảo IEnumerator GetEnumerator Trả IEnumerator • Chú ý: List thay object kiểu tương ứng tạo List 85 Collections Queue • Queue thực collection FIFO • Phương thức • void Enqueue(object): Thêm vào queue • object Dequeue() Lấy khỏi queue • object Peek(): Trả phần tử đầu queue (nhưng không xóa khỏi queue) • Property: • int Count • Phương thức khác: Clear(), Clone(), Contains(), CopyTo(), GetEnumerator(), … 86 Collections Stack • Stack thực collection LIFO • Phương thức • void Push(object): Thêm vào stack • object Pop(): Lấy khỏi stack • object Peek(): Trả phần tử đỉnh stack (nhưng không xóa khỏi stack) • Property: • int Count • Phương thức khác: Peek(), Clear(), Clone(), Contains(), CopyTo(), GetEnumerator(), … 87 Collections Tự Tạo collection • Iteration • foreach hoạt động nào? • Làm dùng foreach với lớp chúng ta? 88 Duyệt phần tử Collection ArrayList a = new ArrayList(); a.Add(1); a.Add(5); a.Add(3); foreach (int x in a) { Console.WriteLine(x); } ArrayList a = new ArrayList(); a.Add(1); a.Add(5); a.Add(3); IEnumerator ie = a.GetEnumerator(); while (ie.MoveNext()) { int x = (int)ie.Current;              Console.WriteLine (x); } 89 Collections Tự Tạo collection • Câu lệnh foreach làm việc collection mà collection thỏa quy tắc sau • Hoặc thực interface IEnumerable • Hoặc thực theo collection pattern: • Collection phải có phương thức public GetEnumerator() không tham số trả kiểu struct, class hay interface • Kiểu trả phương thức GetEnumerator() phải chứa • Phương thức public MoveNext() không tham số, kiểu trả bool • Phương thức public Current không tham số, kiểu trả kiểu tham chiếu 90 Collections Tự Tạo collection class MyCollectionClass: IEnumerable {   } foreach (string s in myCollection) { Console.WriteLine("String is {0}", s); } IEnumerator ie = (IEnumerable) myCollection; IEnumerator e = ie. GetEnumerator();   while (e.MoveNext()) { string s = (string) e.Current; Console.WriteLine("String is {0}", s);  } 91 Collections Tự Tạo collection public interface IEnumerable { IEnumerator GetEnumerator(); } public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); } 92 Collections Tự Tạo collection class MyCollectionClass : IEnumerable { CollectionPart[] _parts; public IEnumerator GetEnumerator() { } } 93 Collections Tự Tạo collection class MyCollectionClass : IEnumerable { CollectionPart[] _parts; public IEnumerator GetEnumerator() { return _parts.GetEnumerator() } } Vì _parts System.Array, kiểu liệt kê nên có phương thức GetEumerator() trả IEnumerator 94 Collections Tự Tạo collection • Giải pháp : tiếp cận dựa mẫu • Trình biên dịch C# compiler tìm kiếm: • GetEnumerator() collection • bool MoveNext() kiểu enumerator • Current kiểu enumerator 95 Q&A 96 ... hủy lớp interface-virtual-override-sealed • Interface tên phương thức • Virtual implement phương thức • Override implement khác phương thức • Sealed implement cuối phương thức 23 Bài tập • Dùng... objMyFile.ApplySecondInterface();    } }  20 Interface Hiện thực • Ý nghĩa cách thực “Chỉ rõ thành viên thuộc interface nào” • Ẩn thành viên bên lớp • Tránh trùng tên đa thừa kế interface 21 Abstract class Interface...Nội dung • Interface • Property, Mảng Indexer • Lớp Collection 22 Interface Interface Khái niệm Interface Khái niệm • Interface định nghĩa “giao kèo” mà thực lớp

Ngày đăng: 15/05/2017, 10:13

Từ khóa liên quan

Mục lục

  • Slide 1

  • Nội dung

  • Slide 3

  • Interface Khái niệm

  • Interface Khái niệm

  • Interface Khai báo

  • Interface Khai báo

  • Interface Khai báo

  • Interface Khai báo

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

  • Interface Hiện thực

Tài liệu cùng người dùng

Tài liệu liên quan