Here does not speak of the concept of generics and the basic knowledge, simply to my understanding of the generic <T>
A. <T>
The following code is simple to use <T> parameters, comments are the interpretation of <T>.
Package xayd.hjj;import Java.util.arraylist;import java.util.List;classa{}classb{}classc{}classD extends c{} Public classTest<t> extends Arraylist<t>{ Public Static<T>voidMain (string[] args) {Test<C> TESTC =NewTest<c>(); //TESTC = new test<b> (); //Compile Error//TESTC = new test<d> (); //Compile Error//Once you specify a class with a parameter of C, you cannot assign the object to a different type, even if the parent classTestc.add (NewC ()); //Testc.add (New B ()); //compile error, cannot store object of Class B unrelated to C in container of Class CTestc.add (NewD ());//compile correctly because the parent class container can place the parent class object, and can also place the subclass objectC testC1= TESTC.Get(0); C testC2= TESTC.Get(1);//takes a subclass object from the parent class container and receives it with the parameter type that was originally passed//D testC3 = testc.get (1); //Compile ErrorD testC4 = (d) testc.Get(1);//takes a subclass object from the parent class container, receives it with the subclass object of the parameter type that was originally passed, and casts the castSystem. out. println (testC1);//[email protected]System. out. println (testC2);//[email protected]System. out. println (TESTC4);//[email protected] }}
In fact, the purpose of inheriting ArrayList is to use the add and get methods, in order to test, you can actually implement a method similar to add and get.
The implementation of the
ArrayList about the add and get methods is as follows:
private transient object[] elementdata; private int size; public Boolean Add (e e) { Ensurecapacityinternal (size + 1 ); Elementdata[size + +] = E; return true ; public E get (int index) {Rangecheck (index); return (E) Elementdata[index]; }
Here's a bit of code similar to the one above. As follows:
Packagexayd.hjj1;Importjava.util.ArrayList;classa{}classb{}classc{}classDextendsc{} Public classTest<t>extendsarraylist{//Note This is not arraylist<t>, in fact should be equivalent to arraylist<object> Public Static voidMain (string[] args) {Test<C> TESTC =NewTest<c>(); //TESTC = new test<d> ();//same compile with errorTestc.add (NewC ()); Testc.add (NewA ());//It's strange that you can compileTestc.add (NewB ());//This can also be done by compilingTestc.add (NewD ()); //C OBJC = testc.get (0); //compilation does not passObject objC1 = testc.get (0);//compiled byc objC2 = (c) testc.get (0);//compiled by//C objC3 = (c) testc.get (1);//such results compile can pass, but the run will undoubtedly throw classcastexceptionA objC4 = (a) testc.get (1);//compiled, but the programmer must know that the second object in the container is type a.Object objC5 = (A) testc.get (1);//compiled byObject objC6 = (object) testc.get (1);//compiled by }}
The difference between this code and the first code is that it is different when inheriting ArrayList. The first piece of code is test<t> extends Arraylist<t>, and this code is test<t> extends ArrayList. It is not safe to see this code from the result of the code running, which is not restricted by the argument passing when calling the Add method with TESTC, not as if the first piece of code would have compiled errors. And the Get method must be used when the type conversion, even if you execute such code C OBJC3 = (c) testc.get (1), the compilation period does not check the error, but at run time throws classcastexception.
Here my explanations for the two different arraylist<t> and ArrayList are as follows (may also be inaccurate, please correct if there are errors):
Test<t> extends Arraylist<t>: test<c> TESTC = new test<c> (), so that the parameter types for Test and ArrayList are C, Therefore, the Add method parameter can only pass subclasses of C and C. From the source of the Add method, you can see that the element is added as follows: elementdata[size++] = e, where Elementdata is actually an array of type object, but because you have limited the parameter of T to the type of Class C, So the compiler will restrict this object to the C type (or its subclass), so it is not possible to compile when you join other objects that are not related to Class C.
Test<t> extends arraylist:test<c> TESTC = new test<c> (), so the Test parameter type is C, and the ArrayList parameter is not passed, So the default is object (the public class Arraylist<e>,arraylist class is implemented in the source code). So as long as the object type can be added, it will lead to the compiler can be unlimited to add any type without any errors, and when the runtime is taken out when the transformation error throws an exception.
Two. The two different results resulting from the above code are due to the implementation of the ArrayList itself. So I implemented a simple unified class to re-interpret T.
Packagexayd.hjj2;Importjava.util.ArrayList;classa{}classb{}classc{}classDextendsc{} Public classTest<t>{ PrivateT obj; PublicT getobj () {returnobj; } Public voidsetobj (T obj) { This. obj =obj; } Public Static<T>voidMain (string[] args) {Test<C> TESTC =NewTest<c>(); Testc.setobj (NewC ()); C OBJC=testc.getobj (); System.out.println (OBJC); //[email protected]//testc.setobj (New B ()); //Compile ErrorTestc.setobj (NewD ());//Compilation succeededC objC1 =testc.getobj (); System.out.println (objC1); //[email protected] }}
This code is simple and clear, the test class and the class member variable parameter types are identical, so there will be no different results.
Java generics <T>