zoukankan      html  css  js  c++  java
  • Programming C#.Interfaces

    类实现一个接口时,它必须实现该接口的所有部分(方法和属性等),效果相当于类要表明:“我同意履行这个接口所定义的协定。”

    从抽象类继承实现了“is-a(是一种)”关系,实现接口时一种“implement(实现)”关系,区别在于:

    举个例子:
    汽车是一种运载工具,它可以实现CanBeBoughtWithABigLoan(可贷巨款购买)这种能力(就像房子一样)
      1 /*************************************************************************************
      2  * 
      3  *  1.如果定义一个Document类,这个类可以存储,又可以压缩,所以要同时实现IStorable和ICompressible接口
      4  *  2.扩展接口就是用一个新街口扩展原来的接口,通过扩展接口我们表示了这样的意思:
      5  *    实现了新接口的任何东西也必须实现原来的接口
      6  *  3.组合接口就是将已有的接口组合起来,并且可以增加新的方法或者属性来创建新的接口
      7  * 
      8  * 
      9  * 
     10  * 
     11  * 
     12  * 
     13  * 
     14  * 
     15  * 
     16  *************************************************************************************/
     17 using System;
     18 
     19 namespace SimpleInterface
     20 {
     21     //第一个接口
     22     interface IStorable
     23     {
     24         //接口的方法声明中没有访问修饰符,隐含是public的,因为接口是要有其他类使用的协定
     25         void Read();
     26         void Write(Object obj);
     27         //属性的声明并未实现get和set方法,只是声明这两个方法
     28         int Status { get; set; }
     29     }
     30 
     31     //第二个接口
     32     interface ICompressible
     33     {
     34         void Compress();
     35         void Decompress();
     36     }
     37 
     38     //扩展接口
     39     interface ILoggedCompressible : ICompressible
     40     {
     41         //新接口增加了一个新的方法记录节省的字节数
     42         void LogSavedBytes();
     43     }
     44 
     45     //组合接口
     46     interface IStorableCompressible : IStorable, ILoggedCompressible
     47     {
     48         //存储压缩前文档的大小
     49         void LogOriginalSize();
     50     }
     51 
     52     public class Document : IStorableCompressible
     53     {
     54         //存储IStorable的Status属性的数据
     55         private int status = 0;
     56 
     57         public Document(string s)
     58         {
     59             Console.WriteLine("Creating Document with:{0}", s);
     60         }
     61 
     62         //实现Read方法
     63         public void Read()
     64         {
     65             Console.WriteLine("Implementing the Read Method for IStorable");
     66         }
     67 
     68         //实现Write方法
     69         public void Write(Object obj)
     70         {
     71             Console.WriteLine("Implemeting the Write Method for IStorable");
     72         }
     73         //实现属性
     74         public int Status
     75         {
     76             get
     77             {
     78                 return status;
     79             }
     80             set 
     81             {
     82                 status = value;
     83             }
     84         }
     85 
     86         //实现ICompressible
     87         public void Compress()
     88         {
     89             Console.WriteLine("Implementing Compress");
     90         }
     91         public void Decompress()
     92         {
     93             Console.WriteLine("Implementing Decompress");
     94         }
     95 
     96         //实现ILoggedCompressible
     97         public void LogSavedBytes()
     98         {
     99             Console.WriteLine("Implementing LogSavedBytes");
    100         }
    101 
    102         //实现IStorableCompressible
    103         public void LogOriginalSize()
    104         {
    105             Console.WriteLine("Implementing LogOriginalSize");
    106         }
    107     }
    108 
    109     //测试接口
    110     public class Tester
    111     {
    112         static void Main()
    113         {
    114             Document doc = new Document("Test Document!");
    115 
    116             //将Document转换为各种接口进行操作
    117             IStorable isDoc = doc as IStorable;
    118             if (isDoc != null)
    119             {
    120                 isDoc.Read();
    121             }
    122             else
    123             {
    124                 Console.WriteLine("IStorable not supported");
    125             }
    126 
    127             ILoggedCompressible ilcDoc = doc as ILoggedCompressible;
    128             if (ilcDoc != null)
    129             {
    130                 //ILoggedCompressible可以调用ICompress接口的方法,因为扩展了该接口
    131                 ilcDoc.Compress();
    132                 ilcDoc.LogSavedBytes();
    133             }
    134             else
    135             {
    136                 Console.WriteLine("ILoggedCompressible not supported");
    137             }
    138 
    139             Console.ReadKey();
    140 
    141         }
    142     }
    143 }
    View Code
    //将Document转换为各种接口进行操作
                IStorable isDoc = doc as IStorable;

    这里的意思是如果不确定类是否实现了某个特定的接口,可以使用as操作符进行转换,然后测试转换的结构是否为null,这样就不用冒着引起异常的风险假定已经转换成功了。当然也可以写成这个样子:

    IStorable isDoc = (IStorable)doc;

    接口与抽象类的比较:

    因为C#不支持多继承,所以接口更好些。但是

    1.飞机会飞,鸟会飞,他们都继承了同一个接口“飞”;但是F22属于飞机抽象类,鸽子属于鸟抽象类。
    2. 就像铁门木门都是门(抽象类),你想要个门我给不了(不能实例化),但我可以给你个具体的铁门或木门(多态);而且只能是门,你不能说它是窗(单继承);一个门可以有锁(接口)也可以有门铃(多实现)。 门(抽象类)定义了你是什么,接口(锁)规定了你能做什么(一个接口最好只能做一件事,你不能要求锁也能发出声音吧(接口污染))。

  • 相关阅读:
    linux下mysql数据导入到redis
    redis常用api
    springboot2.0+mybatis多数据源集成
    斐波那契数列(递归、非递归算法)
    从尾到头打印链表
    docker学习笔记:简单构建Dockerfile【Docker for Windows】
    python3+OpenGL环境配置
    【python库安装问题解决】UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 121: invalid start byte
    DA-GAN技术【简介】【机器通过文字描述创造图像】
    洛谷 P2045 方格取数加强版【费用流】
  • 原文地址:https://www.cnblogs.com/stemon/p/4097727.html
Copyright © 2011-2022 走看看