首页 > 代码库 > Generics
Generics
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;}}
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。