zoukankan      html  css  js  c++  java
  • C# Interface

    1.Interface 定义结构

    1 [attributes] [access-modifiers] interface interface-name [:base-inteface-list] 
    2 { 
    3        interface body 
    4 }

    [attributes] 性质 (可选项);

    [access-modifiers] 访问修饰符 (可选项);//public  internal  private   protected  protected internal;

    interface-name 接口名  (习惯以大写 I 开头);

    [:base-inteface-list] 接口基类列表 (可选项);

    interface body  接口主体(不能有访问修饰符);//只是定义接口的方法和属性等的签名;

                           //实际的实现是写在使用该接口的class里;

    1 //Define an Interface
    2 public interface IStorable
    3 {
    4     void Read( ); //不能有access-modifier
    5     void Write(object);//只是签名,没有具体内容
    6 }

    类继承接口

    1 public class Document : IStorable // 继承IStorable接口
    2 {
    3     public void Read( ) {...} //可以有access-modifier
    4     public void Write(object obj) {...} // 写出具体的实现内容
    5     // ...
    6 }

    一个类继承多个接口

     1 public interface IStorable
     2 {
     3     void Read( );
     4     void Write(object);
     5 }
     6 
     7 interface ICompressible
     8 {
     9     void Compress();
    10     void Decompress();
    11 }
    12 
    13 public class Document : IStorable, ICompressible // 继承多个接口的列表
    14 {
    15     //实现IStorable
    16     public void Read( ) {...} //写出具体的实现内容
    17     public void Write(object obj) {...}
    18     // 实现ICompressible
    19     public void Compress(){ …}
    20     public void Decompress() { …}
    21 }

    接口继承接口

    
    
    interface ICompressible
    {
        void Compress();
        void Decompress();
    }
    interface ILoggedCompressible 
    : ICompressible  // 接口 ILoggedCompressible继承 接口ICompressible
    {
        void LogSavedBytes();
    }
    
    public class Document : ILoggedCompressible 
    {
        // 实现ICompressible
        public void Compress(){ …}
        public void Decompress() { …}
        // 实现ILoggedCompressible 
        public void LogSavedBytes() { …}  // 要实现所有的接口成员
    }
    
    
    
     

    显式定义与隐式定义

     1  interface IStorable
     2  {
     3        void Read();
     4        void Write();
     5  }
     6  interface ITalk
     7  {
     8         void Talk();
     9         void Read();
    10  }
    11  public class Document : IStorable, ITalk
    12  {
    13     public Document(string s)
    14      {…; }
    15      // Make read virtual   定义为虚函数
    16      public virtual void Read() //Istorable的Read方法为 隐式定义
    17      {…; }            
    18      public void Write()
    19      {…; }
    20      void ITalk.Read()  // ITalk的 Read方法 为 显式定义  不能有 访问修饰符  隐式为public
    21      { …; }
    22      public void Talk() //不需要显式定义
    23      { …; }
    24 }    

    显式定义与隐式定义的调用

      1 public class Document : IStorable, ITalk

    2 {
     3     public void Read()
     4      { …; }     …
     5     void ITalk.Read()
     6      { …; }
     7 }
     8 
     9  Document theDoc = new Document(“Test”);
    10 theDoc.Read();   //调用隐式定义的Read()
    11 12 ITalk itDoc = theDoc; // 转换为interface 13 itDoc.Read(); //调用显式定义的Read()
  • 相关阅读:
    IDEA中Git实战
    高并发下redis缓存穿透问题解决方案
    springboot整合mybatis+generator
    Github修改项目显示的语言类型
    Github中README.md换行
    maven中可以直接引用的java系统属性和环境变量属性
    springboot启动异常java.lang.NoSuchFieldError: DEFAULT_INCOMPATIBLE_IMPROVEMENTS
    java获取Linux持续运行时间及友好显示
    http工具类
    idHTTP最简洁的修改和取得Cookie例子
  • 原文地址:https://www.cnblogs.com/yi-jie/p/4395934.html
Copyright © 2011-2022 走看看