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

96 9 0
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 2 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] [attributes][modifiers]interface[:baseList] {{     [interface-body]     [interface-body] }} Interface Khai báo • Tên Interface • Đặt giống tên lớp • Ký tự “I” interface IControl IControl interface {{ void Paint(); Paint(); void }} 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 IControl interface {{ void Paint(); Paint(); void }} interface ITextBox: ITextBox: IControl IControl interface {{ void SetText(string SetText(string text); text); void }} interface IListBox: IListBox: IControl IControl interface {{ void SetItems(string[] SetItems(string[] items); items); void }} interface IComboBox: IComboBox: ITextBox, ITextBox, IListBox IListBox {} {} interface 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); listName.Add(data);  Truy cập liệu listName[index]; listName[index]; foreach (type (type variable variable in in listName) listName) foreach {{ xử lý lý variable variable xử }} 82 Collections ArrayList/List • Xóa liệu listName.Remove(data); listName.Remove(data); listName.Clear(); listName.Clear();  Chèn liệu listName.Insert(index, data); data); listName.Insert(index,  Sắp xếp listName.Sort(); listName.Sort(); 83 Collections ArrayList/List • Tìm kiếm liệu bool listName.Contains(data); listName.Contains(data); bool int listName.IndexOf(data); listName.IndexOf(data); int int listName.LastIndexOf(data); listName.LastIndexOf(data); int 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 ArrayList a.Add(1); a.Add(1); new ArrayList(); ArrayList(); aa == new a.Add(5); a.Add(3); a.Add(3); a.Add(5); foreach (int (int xx in in a) a) foreach {{ Console.WriteLine(x); Console.WriteLine(x); }} ArrayList aa == new new ArrayList(); ArrayList(); ArrayList a.Add(1); a.Add(5); a.Add(5); a.Add(3); a.Add(3); a.Add(1); IEnumerator ie ie == a.GetEnumerator(); a.GetEnumerator(); IEnumerator while (ie.MoveNext()) (ie.MoveNext()) while {{ int xx == (int)ie.Current; (int)ie.Current; int Console.WriteLine (x); (x); Console.WriteLine }} 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 class {{ MyCollectionClass: IEnumerable IEnumerable MyCollectionClass: }} foreach (string (string ss in in myCollection) myCollection) foreach {{ Console.WriteLine("String is is {0}", {0}", s); s); Console.WriteLine("String }} IEnumerator IEnumerator IEnumerator IEnumerator ie == (IEnumerable) (IEnumerable) myCollection; myCollection; ie ie GetEnumerator(); GetEnumerator(); ee == ie while (e.MoveNext()) (e.MoveNext()) while {{ string ss == (string) (string) e.Current; e.Current; string Console.WriteLine("String is is {0}", {0}", s); s); Console.WriteLine("String }} 91 Collections Tự Tạo collection public interface interface IEnumerable IEnumerable public {{ IEnumerator GetEnumerator(); GetEnumerator(); IEnumerator }} public interface interface IEnumerator IEnumerator public {{ object Current Current {{ get; get; }} object bool MoveNext(); MoveNext(); bool void Reset(); Reset(); void }} 92 Collections Tự Tạo collection class MyCollectionClass MyCollectionClass :: IEnumerable IEnumerable class {{ CollectionPart[] _parts; _parts; CollectionPart[] public IEnumerator IEnumerator GetEnumerator() GetEnumerator() public {{ }} }} 93 Collections Tự Tạo collection class MyCollectionClass MyCollectionClass :: IEnumerable IEnumerable class {{ CollectionPart[] _parts; _parts; CollectionPart[] public IEnumerator IEnumerator GetEnumerator() GetEnumerator() public {{ return _parts.GetEnumerator() _parts.GetEnumerator() return }} }} 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 96 ... arrayName=={v1, {v1,v2, v2,…, …,vn}; vn}; type type[[]]arrayName arrayName==new newtype type[[]{v1, ]{v1,v2, v2,…, …,vn}; vn}; type type[[]]arrayName arrayName==new newtype type[n]{v1, [n]{v1,v2, v2,…, …,vn};... {v1,v2,…,vn}, {v1,v2,…,vn}, …, …, {v1,v2,…,vn}}; {v1,v2,…,vn}}; type[,] [,]arrayName arrayName==new newtype[,] type[,]{…}; {…}; type type[,] [,]arrayName arrayName==new newtype[n1, type[n1,n2] n2]{…};... 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

Ngày đăng: 08/05/2021, 11:55

Mục lục

  • Abstract class và Interface

  • Property Property và Field

  • Property Property và Phương thức truy cập

  • Property Property và Phương thức truy cập

  • Property Property và Phương thức truy cập

  • Mảng Bộ khởi tạo

  • Mảng Bộ khởi tạo

  • Mảng Truy cập các phần tử

  • Mảng Mảng giá trị - Mảng tham chiếu

  • Mảng Mảng giá trị - Mảng tham chiếu

  • Mảng Mảng giá trị - Mảng tham chiếu

  • Mảng Mảng và phương thức

  • Mảng Sao chép Mảng

  • Mảng Sao chép Mảng

  • Mảng Sao chép Mảng

  • Mảng Sao chép Mảng

  • Indexer Indexer có nhiều tham số

  • Indexer Indexer có nhiều tham số

  • Một số lớp/cấu trúc cơ bản

  • Collections Tự Tạo collection

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

Tài liệu liên quan