zoukankan      html  css  js  c++  java
  • 不忘本~接口

    返回目录

    接口无疑是面向对象的程序设计中最重要的概念之一,它体现一种抽象,一种规范;即它只规定了具体的操作,而不对操作进行实现,它的实现由继承它的类去完成;它是一种规范,即,它要求实现它的类要对它的成员完全实现才行.

    看一段代码:

     1     /// <summary>
     2 
     3     /// 武器攻击的通用接口
     4 
     5     /// </summary>
     6 
     7     public interface IAttack
     8 
     9     {
    10 
    11         /// <summary>
    12 
    13         /// 攻击方法,被攻击对象是一个敌人对象
    14 
    15         /// </summary>
    16 
    17         /// <param name="Enemy"></param>
    18 
    19         VCommons.VMessage Attack(Entity.Enemy Enemy);
    20 
    21     }

    对接口进行实现,看代码:

     1     /// <summary>
     2 
     3     /// 木头攻击
     4 
     5     /// </summary>
     6 
     7     public class WoodAttack : Entity.IAttack
     8 
     9     {
    10 
    11  
    12 
    13         #region IAttack Members
    14 
    15  
    16 
    17         public VCommons.VMessage Attack(Entity.Enemy enemy)
    18 
    19         {
    20 
    21             return enemy.Notify(20);
    22 
    23         }
    24 
    25  
    26 
    27         #endregion
    28 
    29     }
    30 
    31     /// <summary>
    32 
    33     /// 铁棒攻击
    34 
    35     /// </summary>
    36 
    37     public class MagicAttack : Entity.IAttack
    38 
    39     {
    40 
    41  
    42 
    43         #region IAttack Members
    44 
    45  
    46 
    47         public VCommons.VMessage Attack(Entity.Enemy enemy)
    48 
    49         {
    50 
    51             return enemy.Notify(50);
    52 
    53         }
    54 
    55  
    56 
    57         #endregion
    58 
    59     }

    除了上面的接口之外,还有一种特殊的接口,它内部没有任何元素,我们称为标记接口。它不是为了调用者的期待而定义,其意图是抽象,将那些不能抽象在一起的类,利用一个标记绑定起来,为其提供统一的接口。标记接口保证了调用方法的一致性。虽然强制类型转换会引入具体依赖,却不会有任何副作用,因为在方法实现中,设计者的期待本身就是要转换的类型.

    看代码:(对象实现统一接口和对象具体实体类)

      1     /// <summary>
      2 
      3     /// 数据库对象统一协调接口
      4 
      5     /// </summary>
      6 
      7     public interface IDataEntity
      8 
      9     {
     10 
     11  
     12 
     13     }
     14 
     15  /// <summary>
     16 
     17     /// 活跃值明细       
     18 
     19     /// </summary>
     20 
     21     public partial class ActiveRecord : IDataEntity
     22 
     23     {
     24 
     25         //初始字段
     26 
     27         #region original field
     28 
     29         
     30 
     31         /// <summary>
     32 
     33         /// 活跃值明细ID
     34 
     35         /// </summary>
     36 
     37         public String ActiveRecordID { get;  set; }
     38 
     39         
     40 
     41         /// <summary>
     42 
     43         /// 所有人ID
     44 
     45         /// </summary>
     46 
     47         public String UserID { get;  set; }
     48 
     49         
     50 
     51         /// <summary>
     52 
     53         /// 54 
     55         /// </summary>
     56 
     57         public Int32 Value { get;  set; }
     58 
     59         
     60 
     61         /// <summary>
     62 
     63         /// 收支类型  [资金流向],0-收入,1-支出
     64 
     65         /// </summary>
     66 
     67         public Entity.FlowType FlowType { get;  set; }
     68 
     69         
     70 
     71         /// <summary>
     72 
     73         /// 备注
     74 
     75         /// </summary>
     76 
     77         public String Note { get;  set; }
     78 
     79         
     80 
     81         /// <summary>
     82 
     83         /// 时间
     84 
     85         /// </summary>
     86 
     87         public System.DateTime Createdate { get;  set; }
     88 
     89         
     90 
     91         /// <summary>
     92 
     93         /// 活跃值明细类类型 [默认为1, 为了以后做扩展使用] 
     94 
     95         /// </summary>
     96 
     97         public Int16 Type { get;  set; }
     98 
     99         
    100 
    101         #endregion
    102 
    103  
    104 
    105         //外延字段
    106 
    107         #region extensional field
    108 
    109  
    110 
    111         #endregion
    112 
    113  
    114 
    115         //构造函数
    116 
    117         #region constructed function
    118 
    119         
    120 
    121         /// <summary>
    122 
    123         /// 新建立的时候构造函数
    124 
    125         /// </summary>
    126 
    127         public ActiveRecord()
    128 
    129         {
    130 
    131         
    132 
    133         }
    134 
    135         
    136 
    137         /// <summary>
    138 
    139         /// 新建立的时候构造函数
    140 
    141         /// </summary>
    142 
    143         /// <param name="_ActiveRecordID">活跃值明细ID</param>
    144 
    145         public ActiveRecord(String _ActiveRecordID )
    146 
    147         {
    148 
    149             this.ActiveRecordID = _ActiveRecordID ;
    150 
    151             
    152 
    153         }
    154 
    155  
    156 
    157         #endregion
    158 
    159  
    160 
    161         //方法
    162 
    163         #region function
    164 
    165  
    166 
    167         #endregion
    168 
    169  
    170 
    171         //重写方法
    172 
    173         #region object overrides
    174 
    175  
    176 
    177         #endregion
    178 
    179     }

    而在操作统一接口调用实体时,我们可以直接写成接口类,而不用写用具体类型

     1     /// <summary>
     2 
     3     /// 通用数据库访问接口
     4 
     5     /// </summary>
     6 
     7     public interface IRepository
     8 
     9     {
    10 
    11         /// <summary>
    12 
    13         /// 根据数据库实体—》更新记录
    14 
    15         /// </summary>
    16 
    17         /// <param name="entity"></param>
    18 
    19         void Update(IDataEntity entity);
    20 
    21    }

    注意,在实现时,我们需要将接口对象进行强类型转换,这在标记接口中是可以的.

     1        public void Update(Entity.IDataEntity entity)
     2 
     3         {
     4 
     5 Entity.UserPointRecord _entity = entity as Entity.UserPointRecord;
     6 
     7 //as 返回 null 而不会引发异常
     8 
     9             if (_entity != null)
    10 
    11             {
    12 
    13                 /// 代码逻辑
    14 
    15             }
    16 
    17             else
    18 
    19             {
    20 
    21                 throw new Exception("请传入UserPointRecord类型的模型化对象");
    22 
    23             }
    24 
    25         }

    好了,讲到这里,您是否对接口有了一种全新的认识呢!哈哈!

    返回目录

  • 相关阅读:
    面向对象之继承
    面向对象
    Python—文件和内建函数 open(),file()
    如何在命令行打开文件夹?
    Anaconda在win10下的安装
    Python—for循环和range()内建函数
    python—基础练习2
    python—数据类型
    python—字符编码
    python—基础练习1
  • 原文地址:https://www.cnblogs.com/lori/p/2097997.html
Copyright © 2011-2022 走看看