Lập trình Windows
Chương 2. Ngơn ngữ lập trình C#
Phần 2
1
Nội dung
• Interface
• Property, Mảng và Indexer
• Lớp Collection
2
2
Interface
Interface
Khái niệm
4
Interface
Khái niệm
• Interface định nghĩa một “giao kèo” mà có thể được hiện
thực bởi các lớp
• Một interface có thể chứa các methods, properties, events,
và indexers
• Interface khơng cung cấp các hiện thực của các thành viên
nó định nghĩa
• Interface là một lớp trừu tượng thuần túy
5
Interface
Khai báo
• Cú pháp
[attributes][modifiers]interface<InterfaceName>[:baseList]
[attributes][modifiers]interface<InterfaceName>[:baseList]
{{
[interface-body]
[interface-body]
}}
6
Interface
Khai báo
• Tên Interface
• Đặt giống như tên lớp
• Ký tự đầu tiên là “I”
interface IControl
IControl
interface
{{
void Paint();
Paint();
void
}}
7
Interface
Khai báo
• Danh sách thừa kế
• Interface chỉ có thể thừa kế từ các interface khác
• Interface có thể thực hiện đ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
8
Interface
Khai báo
• Các thành phần bên trong interface
• Phương thức
• Property
• Indexer
• Event
• Và các thành phần khác được thừa kế từ các interface
• Nhận xét
•
•
•
•
Khơng có field trong interface
Khơng có constructor và destructor
Khơng có kiểu lồng nhau
Khơng được viết access modifier cho các thành viên
9
Interface
Hiện thực
• Quy tắc
• Một lớp có thể hiện thực một hay nhiều interface
• Khi một lớp cài đặt interface phải cài đặt mọi thành viên trong các interface
đó
• Cách hiện thực
• Dùng thành viên public
• Hoặc chỉ rõ thành viên thuộc interface nào (tránh tạo thành viên public)
10
Interface
Hiện thực
• Cú pháp
class ClassName:
ClassName:
class
{{
public <type>
<type>
public
1
{…}
{…}
2
InterfaceList
InterfaceList
InterfaceMember()
InterfaceMember()
<type> InterfaceName.InterfaceMember()
InterfaceName.InterfaceMember()
<type>
{…}
{…}
}}
11
Interface
Hiện thực
interfaceIControl
IControl
interface
• Cách 1
{{
voidPaint();
Paint();
void
}}
interfaceIDataBound
IDataBound
interface
{{
voidBind(Binder
Bind(Binderb);
b);
void
}}
publicclass
classEditBox:
EditBox:IControl,
IControl,IDataBound
IDataBound
public
{{
publicvoid
voidPaint()
Paint()
public
{{
…
…
}}
publicvoid
voidBind(Binder
Bind(Binderb)
b)
public
{{
...
...
}}
}}
12
Interface
Hiện thực
• Khi một lớp hay một struct hiện thực một interface thì các instances
của lớp/struct đó có thể được chuyển ngầm định sang kiểu interface
đó
EditBox editBox
editBox == new
new EditBox();
EditBox();
EditBox
editBox.Paint();
editBox.Paint();
IControl control
control == editBox;
editBox;
IControl
IDataBound dataBound
dataBound == editBox;
editBox;
IDataBound
control.Paint();
control.Paint();
dataBound.Bind(…);
dataBound.Bind(…);
13
Interface
Hiện thực
interface IControl
IControl
interface
• Cách 2
{{
void Paint();
Paint();
void
}}
interface IDataBound
IDataBound
interface
{{
void Bind(Binder
Bind(Binder b);
b);
void
}}
public class
class EditBox:
EditBox: IControl,
IControl, IDataBound
IDataBound
public
{{
public void
void IControl.Paint()
IControl.Paint()
public
{{
……
}}
public void
void IDataBound.Bind(Binder
IDataBound.Bind(Binder b)
b)
public
{{
...
...
}}
}}
14
Interface
Hiện thực
• Chú ý: các thành viên hiện thực bằng cách 2 chỉ có thể truy cập qua
kiểu interface
EditBox editBox
editBox == new
new
EditBox
EditBox();
EditBox();
editBox.Paint();
editBox.Paint();
IControl control
control == editBox;
editBox;
IControl
control.Paint();
control.Paint();
15
Interface
Hiện thực
public interface
interface IFile
IFile
public
{{
int DeleteFile();
DeleteFile();
int
void DisplayFile();
DisplayFile();
void
}}
public class
class MyFile
MyFile :: IFile
IFile
public
{{
public int
int DeleteFile()
DeleteFile()
public
{{
Console.WriteLine("DeleteFile Implementation!");
Implementation!");
Console.WriteLine("DeleteFile
return(0);
return(0);
}}
public void
void DisplayFile()
DisplayFile()
public
{{
Console.WriteLine("DisplayFile Implementation!");
Implementation!");
Console.WriteLine("DisplayFile
}}
}}
16
Interface
Hiện thực
class InterfaceDemo
InterfaceDemo
class
{{
public static
static void
void Main()
Main()
public
{{
MyFile objMyFile
objMyFile == new
new MyFile();
MyFile();
MyFile
objMyFile.DisplayFile();
objMyFile.DisplayFile();
int retValue
retValue == objMyFile.DeleteFile();
objMyFile.DeleteFile();
int
}}
}}
17
Interface
Hiện thực
• Hiện thực nhiều interface
public interface
interface IFileTwo
IFileTwo
public
{{
void ApplySecondInterface();
ApplySecondInterface();
void
}}
18
Interface
Hiện thực
public class
class MyFile:
MyFile: IFile,
IFile, IFileTwo
IFileTwo
public
{{
public int
int DeleteFile()
DeleteFile()
public
{{
Console.WriteLine ("DeleteFile
("DeleteFile Implementation!");
Implementation!");
Console.WriteLine
return(0);
return(0);
}}
public void
void DisplayFile()
DisplayFile()
public
{{
Console.WriteLine("DisplayFile Implementation!");
Implementation!");
Console.WriteLine("DisplayFile
}}
public void
void ApplySecondInterface()
ApplySecondInterface()
public
{{
Console.WriteLine("ApplySecondInterface Implementation!");
Implementation!");
Console.WriteLine("ApplySecondInterface
}}
}}
19
Interface
Hiện thực
class MultipleInterfaces
MultipleInterfaces
class
{{
static void
void Main()
Main()
static
{{
MyFile objMyFile
objMyFile == new
new MyFile();
MyFile();
MyFile
objMyFile.DisplayFile();
objMyFile.DisplayFile();
int retValue
retValue == objMyFile.DeleteFile();
objMyFile.DeleteFile();
int
objMyFile.ApplySecondInterface();
objMyFile.ApplySecondInterface();
}}
}}
20
Interface
Hiện thực
• Ý nghĩa của cách hiện thực “Chỉ rõ thành viên thuộc interface nào”
• Ẩn thành viên đối với bên ngồi lớp
• Tránh trùng tên trong đa thừa kế interface
21
Abstract class và Interface
Abstract class
Có thành viên abstract và
khơng abstract
Interface
Tất cả thành viên ngầm
định là abstract
Định nghĩa lớp abstract có các thành viên abstract =
định nghĩa interface
Có thể có các phần
protected, phương thức…
Thành viên của interface là
public khơng có hiện thực
Chỉ được thừa kế từ 1 lớp
abstract
Một lớp có thể thừa kế từ 1
hay nhiều interfaces
Lớp abstract có thể thêm
Tạo thêm chức năng sẽ
nhiều chức năng mà không ảnh hưởng đến lớp con
phá hủy các lớp con
interface-virtual-override-sealed
• Interface chỉ ra tên của phương thức
• Virtual là implement đầu tiên của phương thức
• Override là implement khác của phương thức
• Sealed là implement cuối cùng của phương thức
23
Bài tập
• Dùng phương thức tĩnh Array.Sort(…) để sắp xếp các đối tượng của
một lớp nào đó
• Hướng dẫn: Để dùng phương thức Sort(…) các phần tử của mảng phải
implement giao diện IComparable
public
public
{{
int
int
}}
interface IComparable
IComparable
interface
CompareTo(object obj);
obj);
CompareTo(object
24
Property – Mảng –
Indexer