What is a generic type?
Generic, which is the parameterized type. As the name implies, the type is parameterized by the original specific type, similar to the variable parameter in the method, when the type is also defined as a Parameter form (which can be called a type parameter), and then the specific type (type argument) is passed in when using/calling.
Why use generics?
Let's look at the following code:
Public classGenerictest { Public Static voidMain (string[] args) {List List=NewArrayList (); List.add ("Qqyumidi"); List.add ("Corn"); List.add (100); for(inti = 0; I < list.size (); i++) {String name= (String) list.get (i);//1System.out.println ("Name:" +name); } }}
In the coding process as above, we found that there are two main problems:
1. When we put an object into the collection, the collection does not remember the type of the object, and when the object is removed from the collection again, the compiled type of the object is changed to type object, but its run-time type is still its own type.
2. As a result, it is necessary to//1 the collection element into the specific target type, and the" Java.lang.ClassCastException "exception is easily encountered.
Use generic post-notation:
Public classGenerictest { Public Static voidMain (string[] args) {/*List List = new ArrayList (); List.add ("Qqyumidi"); List.add ("corn"); List.add (100); */List<String> list =NewArraylist<string>(); List.add ("Qqyumidi"); List.add ("Corn"); //List.add (100); //1 hint compilation error for(inti = 0; I < list.size (); i++) {String name= List.get (i);//2System.out.println ("Name:" +name); } }}
after a generic notation, a compilation error occurs when you want to add an integer type Object at//1, through List<string> The list collection can only contain elements of type string, so there is no need to force type conversions at//2, because at this point the collection remembers the type information of the element and the compiler has been able to confirm that it is of type string.
What is the reflection mechanism?
&NBSP;
The reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods and properties; This dynamically acquired information and the ability to dynamically invoke the method of the object is called the reflection mechanism of the Java language.
What can the reflection mechanism do?
The reflection mechanism mainly provides the following functions:
-
Determine the class to which any object belongs at run time;
-
Constructs an object of any class at run time;
-
at runtime, determine the member variables and methods that any class has;
-
method to invoke any object at run time;
-
Generates a dynamic agent.
Practical case of reflection mechanism
Use the reflection mechanism to get the class's method name, return value, parameters, code as follows
PackageFanxingtext;ImportJava.lang.reflect.Method;Importjava.util.ArrayList;Importjava.util.List; Public classFxtest { Public Static voidMain (string[] args) {List L=NewArraylist<>(); Pointclassinfo (l); } Public Static voidpointclassinfo (Object o) {method[] methods=(O.getclass ()). GetMethods (); for(Method method:methods) {System.out.print (Method.getreturntype (). GetName ()+" "); System.out.print (Method.getname ()+" " + "("); for(Class c:method.getparametertypes ()) {System.out.print (C.getname () )+ ","); } System.out.println (")"); } }}
Use the reflection mechanism to hold an integer type object in the ArrayList of the generic string, with the following code:
PackageFanxingtext;ImportJava.lang.reflect.Method;Importjava.util.ArrayList;Importjava.util.List; Public classSetarraylist { Public Static voidMain (string[] args)throwsException {setarraylistotherparam (); } @SuppressWarnings ({"Rawtypes", "unchecked" }) Public Static voidSetarraylistotherparam ()throwsexception{List<String> strings =NewArraylist<string>(); System.out.println (Strings.add ("111")); Class C=Strings.getclass (); Method m= C.getmethod ("Add", Object.class); M.invoke (Strings,1000); for(Object o:strings) {System.out.println (o); } }}
Generics and reflection mechanism in Java