Pan-Reflective analysis

Source: Internet
Author: User
Tags comparable

1.

TypeIt is a public interface of all types. Includes primitive types, parameterized types, array types, type variables, and base types. Parameterizedtype, typevariable, wildcardtype,genericarraytype these four interfaces are its sub-interfaces.
genericdeclaration
This interface Class, method, Constructor are implemented, we are going to use this interface Gettypeparameters method, it returns a typevariable[] array, this array is our definition of the type of variables T and K, Shun The same order as we declare. If you print the array with a looping statement, you will find that only the T and K are output, which is not the result we want, so what do we do to get the expected results? Please keep looking down.
typevariable
It represents a type variable.  such as T, such as K extends Comparable< Super T> & Serializable, this interface has a getbounds () method that is used to get the type array of the upper bound of the types variable, and if there is no upper bound, the default setting is Object, please note typevariable Is the interface, actually get the Typevariableimpl implementation class, the following several interfaces are the same.
Take T and K to illustrate that T does not define any upper bound, so it has a default upper Java.lang.Object, the actual tracking code when you will find that the bounds property of T is empty, only after calling the GetBounds () method, there will be a type[1] array [ Class Java.lang.Object]. For K, when the GetBounds method is called, the resulting array is [java.lang.comparable<? super T> interface Java.io.Serializable], and their types are different. The 1th one is Parameterizedtype, and the second one is class.
Parameterizedtype
Parameterizedtype represents the parameterized type, which is said above Java.lang.comparable< Super T> and, again, List<t>,list<string> these are called parameterized types. Get comparable<? After Super t>, call Getrawtype () and Getactualtypearguments () two methods to get a class (java.lang.Comparable) that declares this parameterized type and an array of actual type arguments ([? Super T]), and this? Super T is also a wildcardtype type.
Wildcardtype
It is used to describe a wildcard expression, which is returned above? Super T is exactly this type. Then call the Getupperbounds () Upper and getlowerbounds () lower bound methods to get the type variable? (Upper and lower bounds), for this case the wildcard (?), which has a upper bound of java.lang.Object and a lower bound of T

Through the analysis of the above several interfaces, you can parse the generic parameters of the person class, then how to deal with the superclass of person and the interface that implements it. The class class also adds the Getgenericsuperclass () and Getgenericinterfaces () two methods in version 1.5 to return the superclass and interface with parameterized types.

2.

Typevariable type variables such as T and K in <T,K>, by Genericdeclaration interface Method Gettypeparameters () get typevariable[]
Parameterizedtype parameterized type is generic like list<string> where List is (type) Rawtype, String is (type[]) actualtypearguments
Wildcardtype wildcard expression type such as? Extends T
Genericarraytype-Generic Array
Genericdeclaration interface, the implementation can declare the type variable and then form a generic

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.