Bài giảng Lập trình trên Windows: Chương 2 - Trần Minh Thái (Phần 2) - Trường Đại Học Quốc Tế Hồng Bàng

20 11 0
Bài giảng Lập trình trên Windows: Chương 2 - Trần Minh Thái (Phần 2) - Trường Đại Học Quốc Tế Hồng Bàng

Đ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

instances của lớp/struct đó có thể được chuyển ngầm định sang kiểu interface đó. EditBox  editBox = new  EditBox (); editBox.Paint();[r]

(1)

Lập trình Windows

Chương Ngơn ngữ lập trình C# Phần 2

(2)

2

• Interface

• Property, Mảng Indexer • Lớp Collection

(3)(4)

4

(5)

5

Khái niệm

• Interface định nghĩa “giao kèo” mà

hiện thực lớp

• Một interface chứa các methods, properties,

events, indexers

• Interface khơng cung cấp thực thành

viên định nghĩa

(6)

6

Khai báo

• Cú pháp

[attributes][modifiers]interface<InterfaceName>[:baseList] {

(7)

7

Khai báo

interface IControl

{

void Paint(); }

• Tên Interface

(8)

8

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); }

(9)

9

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 nhau

(10)

10

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

(11)

11

Hiện thực

• Cú pháp

class ClassName: InterfaceList

{

public <type> InterfaceMember() {…}

<type> InterfaceName.InterfaceMember() {…}

(12)

12

Hiện thực

interface IControl

{

void Paint(); }

interface IDataBound

{

void Bind(Binder b); }

public class EditBox: IControl, IDataBound

{

public void Paint()

{

… }

public void Bind(Binder b)

{

}

}

(13)

13

Hiện thực

• Khi lớp hay struct thực interface

instances lớp/struct chuyển ngầm định sang kiểu interface

EditBox editBox = new EditBox(); editBox.Paint();

IControl control = editBox;

IDataBound dataBound = editBox; control.Paint();

(14)

14 Hiện thực interface IControl { void Paint(); }  interface IDataBound {

void Bind(Binder b); }

public class EditBox: IControl, IDataBound

{

public void IControl.Paint()  {

… }

public void IDataBound.Bind(Binder b)  {

}

(15)

15

Hiện thực

• Chú ý: thành viên thực cách truy

cập qua kiểu interface

EditBox editBox = new  EditBox();

editBox.Paint(); 

(16)

16

Hiện thực

public interface IFile

{

   int DeleteFile();

   void DisplayFile();

}

public class MyFile : IFile

{

   public int DeleteFile()

   {

      Console.WriteLine("DeleteFile Implementation!");

      return(0);

   }

public void DisplayFile()

   {

      Console.WriteLine("DisplayFile Implementation!");

(17)

17

Hiện thực

class InterfaceDemo

{

   public static void Main()    {

       MyFile objMyFile = new MyFile();         objMyFile.DisplayFile();

       int retValue = objMyFile.DeleteFile();    }

(18)

18

Hiện thực

• Hiện thực nhiều interface

public interface IFileTwo

{

(19)

19

Hiện thực

public class MyFile: IFile, IFileTwo

{

   public int DeleteFile()

   {

      Console.WriteLine ("DeleteFile Implementation!");

      return(0);   

   }

   public void DisplayFile()

   {

      Console.WriteLine("DisplayFile Implementation!");

   }

   public void ApplySecondInterface()

   {

     Console.WriteLine("ApplySecondInterface Implementation!");

(20)

20

Hiện thực

class MultipleInterfaces

{

    static void Main() {

MyFile objMyFile = new MyFile(); objMyFile.DisplayFile();

int retValue = objMyFile.DeleteFile(); objMyFile.ApplySecondInterface();    }

Ngày đăng: 01/04/2021, 16:54

Từ khóa liên quan

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

Tài liệu liên quan