1 / 12

Ngôn ngữ C#

Ngôn ngữ C#. Giao diện (Interface). Như một bản hợp đồng, đảm bảo hoạt động tốt theo hoạch định. Giống như class nhưng chỉ gồm toàn các hàm trừu tượng. Khi một class thiết đặt (implement) một giao diện thì phải thi công tất cả các hàm của giao diện này. Thiết đặt một giao diện.

slone
Download Presentation

Ngôn ngữ C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ngôn ngữ C# Ngôn ngữ C#

  2. Giao diện (Interface) • Như một bản hợp đồng, đảm bảo hoạt động tốt theo hoạch định. • Giống như class nhưng chỉ gồm toàn các hàm trừu tượng. • Khi một class thiết đặt (implement) một giao diện thì phải thi công tất cả các hàm của giao diện này. • Thiết đặt một giao diện. • Truy xuất các hàm giao diện. • Override một thiết đặt giao diện. • Dùng giao diện như thông số • Thiết đặt kế thừa giao diện. • Thiết đặt lại giao diện. Ngôn ngữ C#

  3. Thiết đặt một giao diện • Cú pháp: [access-modifier] interface interface-name [:base-list] { interface body } • Tên giao diện nên bắt đầu bằng chử I hoa. • Các thành viên trong phần thân bao gồm: methods, property, events, indexers. • Không thể chứa: hằng, mục tin, tác tử, constructor, destructor, kiểu dữ liệu. • Các thành viên không thể chứa các từ khóa: abstract, public, protected, internal, private, virtual, override. Ngôn ngữ C#

  4. Thiết đặt một giao diện interface IStorable { void Read( ); void Write( object obj); int Status { get ; set ;} }//End interface Istorable publicclass Document : IStorable{ public void Read( ){ Console.WriteLine( “Thi công hàm Read cùa IStorable” ); } public void Write( object obj ){ Console.WriteLine( obj ); } public int Status { get { return Status; } set { Status = value; } } private int Status; }//End class Document Ngôn ngữ C#

  5. Thiết đặt một giao diện • Thiết đặt cùng lúc nhiều giao diện interface ICompressible { void Compress ( ); void Decompress ( ); }//End interface ICompressible publicclass Document : IStorable , ICompressible { //Thicông giao diện IStorable public void Read( ){…..} public void Write( object obj ){….} public int Status {….} //Thi công giao diện ICompressible public void Compress( ) {….} public void Decompress( ) {….} private int Status; } //End class Document Ngôn ngữ C#

  6. Thiết đặt một giao diện • Phối hợp cùng lúc nhiều giao diện và mở rộng interface IStorableCompressible : IStorable , ICompressible { void LogSavedBytes( ); }//End interface IStorableCompressible publicclass Document : IStorableCompressible { // Istorable public void Read( ){…..} public void Write( object obj ){….} public int Status {….} // ICompressible public void Compress( ) {….} public void Decompress( ) {….} // Mở rộng void LogSavedBytes( ) {….} private int Status; } // End class Document Ngôn ngữ C#

  7. Truy xuất các hàm của Giao diện • Truy xuất các thành viên của giao diện IStorable giống như thành viên của lớp Document. Document doc = new Document ( ); doc.Status = -1; doc.Read( ); //Tạo thể hiện bàng cách ép IStorable iStor = (IStorable)doc; iStor.Status = 0; iStor.Read( ); • Không thể tạo thể hiện giao diện một cách trực tiếp.  Tạo thể hiện của giao diện bằng cách ép (casting) đối tượng Document về kiểu giao diện IStorable. Ngôn ngữ C#

  8. Truy xuất các hàm của Giao diện • Toán tử is: • Cú pháp: expression is type • Kiểm tra xem expression (kiểu dữ liệu qui chiếu) có thể ép an toàn về kiểu type không. Document doc = new Document ( ); if( doc is IStorable ) IStorable iStor = (IStorable) doc; Document doc = new Document ( ); IStorable iStor = doc as IStorable; if ( iStor != null ) iStor.Read( ); • Toán tử as: • Cú pháp: expression as type • Kiểm tra xem expression có thể ép an toàn về kiểu type không. Nếu hợp lệ sẽ trả về type. Ngược lại trả về null Ngôn ngữ C#

  9. Truy xuất các hàm của Giao diện • Interface và Abstract • Abstract cũng có thể thiết đặt một Interface nên cũng phải thi công tất cả các thành viên của các Interface. • Được phép ánh xạ các hàm của Interface thành các hàm trừu tượng. interface IStorable{ void Read ( ); void Write (object obj); } abstractclass AbsClass : IStorable{ public abstract void Read ( ); public abstract void Write (object obj); } Ngôn ngữ C#

  10. Override thiết đặt Giao diện interface IStorable{ void Read ( ); void Write (object obj); } //End IStorable publicclass Note : Document { public override void Read ( ){ ………… } public void Write (object obj){ …………. } } //End Note publicclass Document : IStorable{ public virtual void Read ( ) {……….} public void Write (object obj) {….} } //End Document Ngôn ngữ C#

  11. Thiết đặt Giao diện tường minh interface IStorable{ void Read ( ); void Write (object obj); } //End IStorable interfaceITalk{ void Read( ); void Talk( ); } publicclass Document : IStorable, ITalk{ //Thi công IStorable public virtual void Read ( ){…} public void Write (object obj){…} //Thi công ITalk void ITalk.Read( ){…….} publicvoid Talk( ){…….} } //End Note Document doc = new Document( ); doc.Read ( ); ITalk iTal = doc as ITalk; if( iTal != null) iTal.Read( ); Ngôn ngữ C#

  12. Hết chương Hỏi & Đáp Ngôn ngữ C#

More Related