You are on page 1of 8

Lp trnh Web

Chng 8:

INTERFACE (Giao Din)

Lp trnh Web

Ni dung
Interface

Truy cp phng thc Interface.


Thc thi override phuong thc Interface. Thc thi tng minh Interface. Bi tp.

Lp trnh Web

Interface
nh ngha Interface

Thc thi mt Interface


Thc thi nhiu Interface M rng Interface Kt hp cc Interface

Lp trnh Web

nh ngha Interface
Interface ging nh l mt lp c s o thun ty.

Interface ch cha khai bo cc method v khng ci t

phn thn ca method Mt class hoc struct m thc thi mt interface th phi nh ngha phn thn ca tt c cc method trong interface . Khng th to instance ca mt interface.

Lp trnh Web

V d khai bo interface
interface IStoreable { void Read(); void Write(); int Status { get; set; } }

Khai bo cc phng thc ca interface

Khai bo Property ca interface

Lp trnh Web

Thc thi mt Interface


class MyStore : IStoreable{ public void Read(){ Console.WriteLine("Read implementation"); } public void Write(){ Console.WriteLine("Write implementation"); } public int Status{ get{ Console.WriteLine("Status Get implementation"); return 0; } set{ Console.WriteLine("Status Set implementation"); } } }
Lp trnh Web

class Tester { static void Main(string[] args) { MyStore objMyStore = new MyStore(); objMyStore.Read(); IStoreable iStoreable = objMyStore; iStoreable.Read(); }

Lp trnh Web

Thc thi nhiu Interface


Mt Class c th thc thi nhiu interface

V d: class MyClass : IMyInterface1,IMyInterface2{} Mt class thc thi interface no th n phi implement tt c cc phng thc khai bo trong interface .

Lp trnh Web

You might also like