c# 第四课 interfaces
生活随笔
收集整理的這篇文章主要介紹了
c# 第四课 interfaces
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
An interface is a contract(協定) that guarantees to a client how a class or struct will behave. When a class implements an interface(實現一個接口), it tells any potential(可能的) client “I guarantee I’ll support all the methods, properties, events, and indexers of the named interface.” These contracts are made manifest(表明) using the interface keyword, which declares a reference type(引用類型) that encapsulates(封裝) the contract. interface ICompressible
{void Compress();void Decompress();
}
interface ILoggedCompressible
: ICompressible
{void LogSavedBytes();
}
public class Document : ILoggedCompressible
{// 實現ICompressiblepublic void Compress(){ …}public void Decompress() { …}// 實現ILoggedCompressible public void LogSavedBytes() { …}
// 要實現所有的接口成員
?
?
as operator
foreach (Document doc in folder) {IStorable isDoc = doc as IStorable;if (isDoc != null){isDoc.Read();}else{Console.WriteLine("IStorable not supported");doc.Encrypt();} … }as 有左右兩個操作數 Casts the left operand to the type specified by the right operand. Returns null if the cast fails. Interface Vs. Abstract Class Interfaces are very similar to abstract classes. C# doesn’t allow multiple inheritance with classes. C# does allow to implement any number of interfaces and derive from one base class.轉載于:https://www.cnblogs.com/GSONG/p/4399062.html
總結
以上是生活随笔為你收集整理的c# 第四课 interfaces的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: win32com 读doc,doc表格
- 下一篇: 融易宝项目之EasyExcel和数据字典