Generic and generic programming

Source: Internet
Author: User

Generic and generic programming

Unit 7th: Generic Type of Java 5
36. Get started with basic generic applications
Generics are provided for javac. You can limit the input parameters in the set so that the compiler blocks the illegal input in the source program.
The "type" information will be removed when the compiler compiles a set with the type description. This means that the efficiency of the program during runtime is not affected.
The returned value of the getClass () method is exactly the same as that of the original type. Because the bytecode generated by compilation removes the generic type information, you only need to skip the compiler, to a generic set, assume that data of other types is used,
For example, reflection to the set, and then call the add method.

ArrayList <String> arr = new ArrayList <String> (); Method = arr. getClass (). getMethod ("add", Object. class); method. invoke (arr, 111); System. out. println (arr. size (); // Error


37. Extensive internal principles and deeper Application



38. wildcard extension application "?" : Applicable to any type. The generic type does not have an inheritance relationship. It can reference other parameterized types. Its Defined variables are mainly used as references and can call Methods unrelated to parameterization, extension of method delimiters related to parameterization cannot be called: the qualified wildcard always includes its own 1. upper limit of a wildcard: Correct: Vector <? Extends Number> x = new Vector <Integer> (); error: Vector <? Extends Number> x = new Vector <String> (); 2. Specify the bottom boundary of the wildcard: Correct: Vector <? Super Integer> x = new Vector <Number> (); error: Vector <? Super Integer> x = new Vector <Byte> (); 40. Custom generic Methods and Their Applications 41. Summary of exercises and types of custom generic methods




42. Application of custom generic classes
43. Obtain the actual type parameters of the generic type through reflection
Public void method2 (ArrayList <String> [] array, HashMap <String, Integer> [] hash, int aa) {} public static void getGenericParameterTypesTest () throws NoSuchMethodException, securityException {Method [] methods = FanxingTest. class. getMethods (); for (Method method: methods) {if (method. getName (). equals ("method2") {// If the parameter Type is parameterized, the Type object returned by the parameter must actually reflect the actual Type used in the source code. // parameter. Create a parameter if it is a type variable or parameterized type. Otherwise, it will be parsed. Type [] types = method. getGenericParameterTypes (); for (int I = 0; I <types. length; I ++) {System. out. println (method. getName () + "========" + types [I]) ;}}} package java_5; import java. lang. reflect. method; import java. lang. reflect. parameterizedType; import java. lang. reflect. type; import java. util. date; import java. util. vector; public class GenericDAO {public static void main (String [] args) throws Exception {Method method = GenericDAO. class. getMethod ("apply", Vector. class); // obtain the generic parameter Type. An array is returned because a method may have multiple generic parameter types [] genericParameter = method. getGenericParameterTypes ();/** here, the method apply only has one generic parameter type, so the parameter is removed as 0. * ParameterizedType indicates a parameterized type, for example, Vector <Date> */ParameterizedType parameterizedType = (ParameterizedType) genericParameter [0]; // print out the original parameter type System. out. println (parameterizedType. getRawType ();/** parameterizedType. getActualTypeArguments () returns an array of the actual type. * because there may be multiple arrays, for example, Map <k, v> has only one, the subscript 0 */System is used. out. println (parameterizedType. getActualTypeArguments () [0]);}/** there is no way to get the type of wildcard vector passed in Through Vector, that is, the Date type cannot be obtained. * The apply method is used to obtain the type of its generic parameter. */public void apply (Vector <Date> vector ){}}


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.