{"id":5220,"date":"2021-10-26T16:29:56","date_gmt":"2021-10-26T08:29:56","guid":{"rendered":"http:\/\/xinyiworld.top\/wordpress\/?p=5220"},"modified":"2021-11-01T18:13:15","modified_gmt":"2021-11-01T10:13:15","slug":"type%e6%8e%a5%e5%8f%a3","status":"publish","type":"post","link":"http:\/\/xinyiworld.top\/wordpress_it\/?p=5220","title":{"rendered":"Type\u63a5\u53e3"},"content":{"rendered":"<p>\u53c2\u8003\u535a\u6587\uff1a<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/cloud.tencent.com\/developer\/article\/1121266?from=14588\">https:\/\/cloud.tencent.com\/developer\/article\/1121266?from=14588<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.jianshu.com\/p\/bc21159c0549\">https:\/\/www.jianshu.com\/p\/bc21159c0549<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/www.cnblogs.com\/baiqiantao\/p\/7460580.html\">https:\/\/www.cnblogs.com\/baiqiantao\/p\/7460580.html<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/u010884123\/article\/details\/78189395?locationNum=6&amp;fps=1\">https:\/\/blog.csdn.net\/u010884123\/article\/details\/78189395?locationNum=6&amp;fps=1<\/a><\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/blog.csdn.net\/kaisnm\/article\/details\/105839169\">https:\/\/blog.csdn.net\/kaisnm\/article\/details\/105839169<\/a><\/p>\n<h3>Type\u5b98\u65b9\u4ecb\u7ecd<\/h3>\n<pre><code class=\"language-java line-numbers\">\/**\n * Type is the common superinterface for all types in the Java\n * programming language. These include raw types, parameterized types,\n * array types, type variables and primitive types.\n *\n * @since 1.5\n *\/\n<\/code><\/pre>\n<p>Type\u662fJava\u4e2d\u6240\u6709\u7c7b\u578b\u7684\u516c\u5171\u8d85\u7ea7\u63a5\u53e3<\/p>\n<p>\u65e0\u8bba\u662f\u7c7b\u3001\u65b9\u6cd5\u3001\u53d8\u91cf\uff0c\u90fd\u53ef\u4ee5\u83b7\u53d6Type\u3002<\/p>\n<h3>Type\u7c7b\u578b<\/h3>\n<p>Type\u6709\u56db\u4e2a\u76f4\u63a5\u5b50\u7c7b\uff1aClass\u3001TypeVariable\u3001WildcardType\u3001ParameterizedType\u3001GenericArrayType<\/p>\n<ul>\n<li>Class<br \/>\n\u5f53\u7c7b\u4e0e\u6cdb\u578b\u65e0\u5173,java\u53ef\u4ee5\u76f4\u63a5\u5c06\u8fd9\u4e2atype\u7c7b\u5f3a\u8f6c\u4e3aClass\u3002<\/p>\n<\/li>\n<li>\n<p>TypeVariable<code>&lt;D extends GenericDeclaration&gt;<\/code><br \/>\n\u7c7b\u578b\u53d8\u91cf, \u8303\u578b\u4fe1\u606f\u5728\u7f16\u8bd1\u65f6\u4f1a\u88ab\u8f6c\u6362\u4e3a\u4e00\u4e2a\u7279\u5b9a\u7684\u7c7b\u578b, \u800cTypeVariable\u5c31\u662f\u7528\u6765\u53cd\u6620\u5728JVM\u7f16\u8bd1\u8be5\u6cdb\u578b\u524d\u7684\u4fe1\u606f.<\/p>\n<\/li>\n<\/ul>\n<p>\u5982\u5bf9\u4e8eList<T>\u7c7b\uff0c\u5b83\u7684TypeVariable\u5c31\u662fT\uff1b\u5bf9\u4e8e<code>ClassDemo&lt;T:List&lt;String&gt;,K,M&gt;<\/code>\uff0c\u5b83\u7684\u6cdb\u578b\u5c31\u662f<code>T,K,M<\/code>\u3002<\/p>\n<p>\u6ce8\u610f\uff1a<strong>TypeVariable\u65e2\u53ef\u4ee5\u6307\u7c7b\u7684\u6cdb\u578b\uff0c\u4e5f\u53ef\u4ee5\u6307\u65b9\u6cd5\u7684\u6cdb\u578b<\/strong>\u3002<\/p>\n<p><strong>-------------\u83b7\u53d6TypeVariable\u7684\u65b9\u6cd5\uff1a-------------<\/strong><\/p>\n<blockquote><p>\n  Class getTypeParameters()\u65b9\u6cd5\n<\/p><\/blockquote>\n<p>\u83b7\u53d6\u7c7b\u4e0a\u58f0\u660e\u7684\u6cdb\u578b<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.yiidian.com\/java-lang\/java-class-gettypeparameters.html\">http:\/\/www.yiidian.com\/java-lang\/java-class-gettypeparameters.html<\/a><\/p>\n<blockquote><p>\n  Method getTypeParameters()\u65b9\u6cd5\n<\/p><\/blockquote>\n<p>\u83b7\u53d6\u65b9\u6cd5\u4e0a\u58f0\u660e\u7684\u6cdb\u578b<\/p>\n<p><strong>-------------TypeVariable\u7684\u65b9\u6cd5\uff1a-------------<\/strong><\/p>\n<blockquote><p>\n  String <strong>getName<\/strong>()\n<\/p><\/blockquote>\n<pre><code>\u5bf9\u4e8e`\"List&lt;T&gt;\"`\uff0c\u83b7\u53d6\u7684\u5c31\u662fT\u3002\n<\/code><\/pre>\n<blockquote><p>\n  Type[] <strong>getBounds<\/strong>()\n<\/p><\/blockquote>\n<pre><code>\u83b7\u53d6\u7c7b\u578b\u53d8\u91cf\u7684\u4e0a\u8fb9\u754c, \u82e5\u672a\u660e\u786e\u58f0\u660e\u4e0a\u8fb9\u754c\u5219\u9ed8\u8ba4\u4e3aObject\n\u5bf9\u4e8eT\uff0c\u83b7\u53d6\u7684\u5c31\u662fObject\n\u5bf9\u4e8e`K : List&lt;String&gt;`\n<\/code><\/pre>\n<blockquote><p>\n  D <strong>getGenericDeclaration<\/strong>()\u65b9\u6cd5\n<\/p><\/blockquote>\n<pre><code>\u4e0b\u9762\u7684\u4ee3\u7801\n<\/code><\/pre>\n<pre><code class=\"line-numbers\">class Test {\n    fun &lt;T,K : List&lt;String&gt;,M&gt; testType(\n        a1: List&lt;String?&gt;?, a2: List&lt;ArrayList&lt;String?&gt;?&gt;?, a3: List&lt;T&gt;?,  \/\/\n        a4: List&lt;Number?&gt;?, a5: List&lt;Array&lt;ArrayList&lt;String?&gt;?&gt;?&gt;?, a6: Map&lt;String?, Int?&gt;?\n    ) {\n    }\n}\n<\/code><\/pre>\n<p>\u5bf9\u4e8e\u7c7bTest\u83b7\u53d6\u7684TypeVariable\uff0c\u6267\u884cgetGenericDeclaration\u65b9\u6cd5\u8fd4\u56de\u7684\u662f<br \/>\n<code>class com.example.reflectdemo.Test<\/code><br \/>\n\u5bf9\u4e8e\u65b9\u6cd5testType\u83b7\u53d6\u7684TypeVariable\uff0c\u6267\u884cgetGenericDeclaration\u65b9\u6cd5\u8fd4\u56de\u7684\u662f<\/p>\n<pre><code class=\"line-numbers\">public final void com.example.reflectdemo.Test.testType(java.util.List,java.util.List,java.util.List,java.util.List,java.util.List,java.util.Map)\n<\/code><\/pre>\n<ul>\n<li>ParameterizedType<br \/>\n<strong>-------------\u83b7\u53d6ParameterizedType\u7684\u65b9\u6cd5\uff1a-------------<\/strong><br \/>\n\u5bf9\u4e8e\u4ee3\u7801\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-Java line-numbers\">open class AbsParameterizedTypeClassDemo&lt;T:List&lt;String&gt;,K,M&gt;{\n    fun test(){\n\n    }\n}\nclass AbsParameterizedTypeClassDemoImpl() :\n    AbsParameterizedTypeClassDemo&lt;List&lt;String&gt;, String, String&gt;() {\n\n}\n<\/code><\/pre>\n<p>getGenericSuperclass\u83b7\u53d6\u5e26\u6cdb\u578b\u7684\u7236\u7c7bType\uff0c\u7c7b\u578b\u4e3aParameterizedTypeImpl\u3002<\/p>\n<pre><code class=\"language-java line-numbers\">@RequiresApi(Build.VERSION_CODES.N)\nfun parameterizedTypeTest(){\n    val type: Type = AbsParameterizedTypeClassDemoImpl::class.java.getGenericSuperclass()\n    println(type)\n    println(\"type class name = ${type.javaClass}\")\n\n    var pt:ParameterizedType = type as ParameterizedType\n    for (item in  pt.actualTypeArguments){\n        println(item)\n    }\n}\n\n<\/code><\/pre>\n<p><strong>-------------ParameterizedType\u7684\u65b9\u6cd5\uff1a-------------<\/strong><\/p>\n<blockquote><p>\n  Type[] <strong>getActualTypeArguments<\/strong>()\n<\/p><\/blockquote>\n<p>\u83b7\u53d6\u53c2\u6570\u5316\u7c7b\u578b\u3002\u901a\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u8be5\u63a5\u53e3\u6765\u68c0\u9a8c\u6cdb\u578b\u662f\u5426\u88ab\u53c2\u6570\u5316\u3002\u901a\u8fc7instance\u5224\u65ad\u662f\u5df2\u7ecf\u53c2\u6570\u5316\u4e86\u4e4b\u540e\uff0c\u518d\u901a\u8fc7getActualTypeArguments\u65b9\u6cd5\u83b7\u53d6\u53c2\u6570\u5316\u7c7b\u578b\u3002<\/p>\n<p>\u8fd4\u56de\u53c2\u6570\u5316\u7c7b\u578b\u7684\u539f\u59cb\u7c7b\u578b\uff0c\u4f8b\u5982List<String> \u7684\u539f\u59cb\u7c7b\u578bList<\/p>\n<p>\u5bf9\u4e8e<code>ClassDemo&lt;List&lt;String&gt;, String, String&gt;<\/code>\uff0c\u83b7\u53d6\u7684\u7ed3\u679c\u5c31\u662f<\/p>\n<pre><code class=\"language-java line-numbers\">java.util.List&lt;? extends java.lang.String&gt;\nclass java.lang.String\nclass java.lang.String\n<\/code><\/pre>\n<blockquote><p>\n  Type <strong>getRawType<\/strong>\n<\/p><\/blockquote>\n<p>\u8fd4\u56de\u53c2\u6570\u5316\u7c7b\u578b\u7684\u539f\u59cb\u7c7b\u578b\uff0c\u4f8b\u5982List<String> \u7684\u539f\u59cb\u7c7b\u578bList<br \/>\n\u83b7\u53d6\u58f0\u660e\u6cdb\u578b\u7684\u7c7b\u6216\u8005\u63a5\u53e3\uff0c\u4e5f\u5c31\u662f\u6cdb\u578b\u4e2d&lt;>\u524d\u9762\u7684\u90a3\u4e2a\u503c\u3002<\/p>\n<blockquote><p>\n  Type <strong>getOwnerType<\/strong>\n<\/p><\/blockquote>\n<p>\u8fd4\u56de\u7c7b\u578b\u6240\u5c5e\u7684\u7c7b\u578b\u3002\u4f8b\u5982A<T>\u7c7b\uff0c\u5176\u4e2d\u5b9a\u4e49\u4e86\u5185\u90e8\u7c7bInnerA<I>,\u5219InnerA<I>\u6240\u5c5e\u7c7b\u578b\u4e3aA<T>,\u5982\u679c\u662f\u9876\u5c42\u7c7b\u578b\u5219\u8fd4\u56denull\u3002\u5982Map.Entry&lt;K,V>\u7684\u6240\u5c5e\u7c7b\u578b\u4e3aMap&lt;K,V>.<br \/>\n- GenericArrayType<\/p>\n<p>\u8868\u793a\u7684\u662f\u6570\u7ec4\u7c7b\u578b\u4e14\u7ec4\u6210\u5143\u7d20\u662fParameterizedType \u6216TypeVariable\u3002\u4f8b\u5982List<String>[] \u6216T[]\u3002\u8be5\u63a5\u53e3\u53ea\u6709Type getGenericComponentType()\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5b83\u8fd4\u56de\u6570\u7ec4\u7684\u7ec4\u6210\u5143\u7d20\u3002<\/p>\n<p>\u5bf9\u4e8e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">open class AbsParameterizedTypeClassDemo{\n    var key1 : Array&lt;M&gt; = TODO()\n    var key2 : Array&lt;List&lt;String&gt;&gt;\n}\n\nfun genericArrayTypeTest(){\n    val type1: GenericArrayType =\n        AbsParameterizedTypeClassDemo::class.java.getDeclaredField(\"key1\").genericType as GenericArrayType\n    println(type1.javaClass)\n    println(type1.genericComponentType)\n\n    val type2: GenericArrayType =\n        AbsParameterizedTypeClassDemo::class.java.getDeclaredField(\"key2\").genericType as GenericArrayType\n    println(type2.javaClass)\n    println(type2.genericComponentType)\n\n}\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">class sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl\nM\nclass sun.reflect.generics.reflectiveObjects.GenericArrayTypeImpl\njava.util.List&lt;java.lang.String&gt;\n<\/code><\/pre>\n<ul>\n<li>WildcardType<br \/>\n\u901a\u914d\u7b26\u6cdb\u578b\uff0c\u5305\u542b2\u4e2a\u65b9\u6cd5\uff1a<\/li>\n<\/ul>\n<blockquote><p>\n  Type[] <strong>getUpperBounds<\/strong><\/p>\n<p>  Type[] <strong>getLowerBounds<\/strong>\n<\/p><\/blockquote>\n<p>\u5bf9\u4e8e\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">public class TestType {\n    private List&lt;? extends Number&gt; a;  \/\/ \/\/ a\u6ca1\u6709\u4e0b\u754c, \u53d6\u4e0b\u754c\u4f1a\u629b\u51faArrayIndexOutOfBoundsException\n    private List&lt;? super String&gt; b;\n    public static void main(String[] args) throws Exception {\n        Field fieldA = TestType.class.getDeclaredField(\"a\");\n        Field fieldB = TestType.class.getDeclaredField(\"b\");\n        \/\/ \u5148\u62ff\u5230\u8303\u578b\u7c7b\u578b\n        ParameterizedType pTypeA = (ParameterizedType) fieldA.getGenericType();\n        ParameterizedType pTypeB = (ParameterizedType) fieldB.getGenericType();\n        \/\/ \u518d\u4ece\u8303\u578b\u91cc\u62ff\u5230\u901a\u914d\u7b26\u7c7b\u578b\n        WildcardType wTypeA = (WildcardType) pTypeA.getActualTypeArguments()[0];\n        WildcardType wTypeB = (WildcardType) pTypeB.getActualTypeArguments()[0];\n        \/\/ \u65b9\u6cd5\u6d4b\u8bd5\n        System.out.println(wTypeA.getUpperBounds()[0]);   \/\/ class java.lang.Number\n        System.out.println(wTypeB.getLowerBounds()[0]);   \/\/ class java.lang.String\n        \/\/ \u770b\u770b\u901a\u914d\u7b26\u7c7b\u578b\u5230\u5e95\u662f\u4ec0\u4e48, \u6253\u5370\u7ed3\u679c\u4e3a: ? extends java.lang.Number\n        System.out.println(wTypeA);\n        System.out.println(wTypeB);\n    }\n}\n<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code class=\"language-java line-numbers\">class java.lang.Number\nclass java.lang.String\n? extends java.lang.Number\n? super java.lang.String\n<\/code><\/pre>\n<button class=\"simplefavorite-button\" data-postid=\"5220\" data-siteid=\"1\" data-groupid=\"1\" data-favoritecount=\"0\" style=\"\">\u6536\u85cf <i class=\"sf-icon-star-empty\"><\/i><\/button>","protected":false},"excerpt":{"rendered":"<p>\u53c2\u8003\u535a\u6587\uff1a https:\/\/cloud.tencent.com\/developer\/article\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[284],"tags":[],"_links":{"self":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5220"}],"collection":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5220"}],"version-history":[{"count":25,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5220\/revisions"}],"predecessor-version":[{"id":5266,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=\/wp\/v2\/posts\/5220\/revisions\/5266"}],"wp:attachment":[{"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5220"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5220"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinyiworld.top\/wordpress_it\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5220"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}