前段時(shí)間我還在納悶,在上上篇設(shè)計(jì)模式的單例模式中,我只New過(guò)一次對(duì)象。但是查看我分明得到了2個(gè)或多個(gè)不同的對(duì)象。今天做個(gè)試驗(yàn)。代碼如下
通過(guò)實(shí)驗(yàn)發(fā)現(xiàn),對(duì)象確實(shí)是new了兩次,initial確實(shí)是進(jìn)入了兩次,對(duì)于不同泛型對(duì)象,是要重新開(kāi)辟內(nèi)存的。
下面的文章做個(gè)資料保存。
http://www.survivalescaperooms.com/DebugLZQ/archive/2012/09/03/2669383.html
http://msdn.microsoft.com/en-US/library/f4a6ta2h(v=vs.80).aspx
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace Consoleapplication10 7 { 8 class PRogram 9 {10 static void Main(string[] args)11 {12 generictest<custom1>.List.Add(new custom1("custom1(1)"));13 generictest<custom1>.List.Add(new custom1("custom1(2)"));14 generictest<custom2>.List.Add(new custom2("custom2(1)"));15 generictest<custom2>.List.Add(new custom2("custom2(2)"));16 Console.ReadKey();17 }18 }19 class generictest<T>20 {21 private generictest()22 { }23 static private List<T> list=null;24 static public List<T> List25 {26 set27 {28 29 list = value;30 }31 get32 {33 if (list == null)34 {35 initial();36 }37 return list;38 }39 }40 41 private static void initial()42 {43 list = new List<T>();44 }45 }46 class custom147 {48 public string str;49 public custom1(string str)50 { 51 this.str=str;52 }53 }54 class custom255 {56 public string str;57 public custom2(string str)58 { 59 this.str=str;60 }61 62 }63 }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注