A generic problem ., Generic problems.

Source: Internet
Author: User

A generic problem ., Generic problems.

Some time ago, I was still wondering that in the previous design mode Singleton mode, I only updated the object once. However, I have obtained two or more different objects. Let's do a test today. The Code is as follows:

Experiments show that the object is indeed new twice, and the initial is indeed in two times. For different generic objects, the memory needs to be re-opened.

Save the following document.

Http://www.cnblogs.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 }

 


Teach a question about java generics

<T extends A> void test (List <T> list); indicates A type installed in the List ..

Void test (List <? Extends A> list); indicates any type ..

The difference is that a certain type is definite ..

Any type is unknown ..

It is reflected in the code ..

You are in <T extends A> void test (List <T> list); in this method .. you can retrieve the data in the list. The data type is T .. adding to it is also .. the T type can be added ..
But in void test (List <? Extends A> list) here... because it can be of any type... he does not know what type... so he cannot add... even the Object is not ..

<T> void test (List <T> list ){
T t = null;
List. add (t );
T t1 = list. get (0 );

}

Void test1 (List <?> List ){
Object obj = list. get (0); // valid
// List. add (new Object (); an error is reported. What is it installed in it? Type... not Object ..
}

Hope to help you ..

A java generic problem

<T extends Collection> the keyword extends is used here. It can be a class or an interface later. However, the extends here is no longer the meaning of inheritance. It should be understood that the T type is the type that implements the Collection interface, or T is the type that inherits the XX class.
I just found it online for you. At the beginning, I didn't see the problem clearly, so I also learned something, huh, huh, baike.baidu.com/view/1436058.htm? Func = retitle this is the URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.