码迷,mamicode.com
首页 > 其他好文 > 详细

Generics

时间:2017-04-21 16:45:21      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:current   methods   des   view   bsp   hid   open   spec   can   

Geniric helps to reuse the methods which avoid overloading methods that will become hard to maintain

Public Class Equalizer

{

    public bool compare (string s1, string s2)

     {return s1 == s2;}

    public bool compare (int i1, int i2)

    {return i1 == i2;}

}

If we have a need to add a cross cutting aspect to all compare method like logging, how would current design accomodate the need

技术分享
Public Class EqualizerWithLogging

{

    public bool compare (string s1, string s2)

     {

        iLoogger.log(XXXX);

       return s1 == s2;

     }

    public bool compare (int i1, int i2)

    {

       iLoogger.log(XXXX);

return i1 == i2;}

}
View Code

By simply resort this to generic class can resolve this issue

Public Class EqualizerWithLogging <T>
{
    public bool compare (T s1, T s2)
     {        iLoogger.log(XXXX);
       return s1 == s2;
     }
}

 

  

Generics

标签:current   methods   des   view   bsp   hid   open   spec   can   

原文地址:http://www.cnblogs.com/Mattcoder/p/6744093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!