C#ã§Generics
C#ã®Genericsã«ã¤ãã¦ã®è©³ãã説æã¯ä»¥ä¸ã®ãµã¤ããåç
§ãï¼è±èªï¼
http://msdn2.microsoft.com/en-us/library/ms379564(VS.80).aspx
C#ã®Genericsã¯Javaã¨å°ãéã£ã¦ãã¾ããã
åºæ¬çã«ã¯Javaã¨åããã¨ã¯ã§ããããã§ãã
詳ããã¯ã¾ã 調ã¹ããã¦ãã¾ãããã
ãã¦ãåã«å¯¾ããåå¼æ°ãæå®ããå ´å以ä¸ã®ããã«ãªãã¾ãã
class Factory <T>{ }
åå¼æ°ã®å¶ç´
åå¼æ°ã®å¶ç´ã®è¨å®ãããã«ã¯ã以ä¸ã®ããã«whereãã¼ã¯ã¼ããå©ç¨ãã¾ãã
ãããããã¨ã§ãåå¼æ°ã«æ¸¡ãã¯ã©ã¹ãAã®ãµãã¯ã©ã¹ã«éå®ã§ãã¾ãã
ï¼C#ã§ã¯extendsãã¼ã¯ã¼ãããã³implementsãã¼ã¯ã¼ãã®ä»£ããã«
ãã³ãã³":"ã使ãããã®ã§ãTã¯Aãç¶æ¿ããã¨ããæå³ã§èªã¿åããã¨ãã§ãã¾ããï¼
class A {} class B extends A {} class Factory <T> where T : A { }
new() å¶ç´
ç§ã使ãããªã¨æã£ãæ©è½ã¯new()å¶ç´ã§ãã
new()å¶ç´ã使ããã¨ã§ãåå¼æ°ããã¤ã³ã¹ã¿ã³ã¹ãçæããæ©è½ãããã¾ãã
ï¼Javaã§ã¯åå¼æ°ããã¤ã³ã¹ã¿ã³ã¹ãä½ããªãã£ãã¨æãã¾ããã調æ»ä¸ï¼
以ä¸ã®ããã«è¨è¿°ãã¾ãã
class Factory <T> where T : new () {}
åå¼æ°ã«å¯¾ããå¶ç´ãçµã¿åãããå ´åã¯ã以ä¸ã®ããã«ã«ã³ãã§åºåãã¾ãã
class Factory <T> where T : A,new () { }
Genericsã®ä½¿ãã©ããã¯Factoryï¼ï¼
å人çã«Genericsã¯ãç¹ã«Factoryãä½ãã¨ããªã©ã«ä¾¿å©ã ã¨æã£ã¦ãã¾ãã
ãã®ããnew()å¶ç´ã¯ããããããæ©è½ã§ãã
ããã使ã£ã¦ä»¥ä¸ã®ããã«Factoryãä½ããã¨ãã§ãã¾ãã
class A {} class B extends A {} class Factory <T> where T : A,new () { public static T createInstance(){ retrun new T(); } }
ãã®Factoryãå©ç¨ããã«ã¯ã以ä¸ã®ããã«è¨è¿°ãã¾ãã
B bins = Factory<B>.createInstance();
ããã«ãã£ã¦ãFactoryã¯ã©ã¹ã¯Bã¯ã©ã¹ã«ã¤ãã¦ç¥ãå¿
è¦ã¯ãªãã
ããããBã¯ã©ã¹ãAã¯ã©ã¹ãç¶æ¿ãã¦ãããã¨ãä¿éã§ãã¾ãã
ãã¬ã¼ã ã¯ã¼ã¯ã¨ãä½ãããããªãã¾ããï¼