{"id":738,"date":"2023-03-23T21:50:23","date_gmt":"2023-03-23T13:50:23","guid":{"rendered":""},"modified":"2023-03-23T21:50:23","modified_gmt":"2023-03-23T13:50:23","slug":"Java \u53cd\u5c04","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/738.html","title":{"rendered":"Java \u53cd\u5c04"},"content":{"rendered":"


\n <\/head>
\n <\/p>\n

\n

Java \u53cd\u5c04<\/h1>\n<\/p><\/div>\n
\n Java Reflection <\/b>\u662f\u4e00\u4e2a\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u6216\u4fee\u6539\u7c7b\u7684\u8fd0\u884c\u65f6\u884c\u4e3a\u7684\u8fc7\u7a0b\u3002\n <\/div>\n
\n java.lang.Class <\/b>\u7c7b\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u6cd5\uff0c\u53ef\u7528\u4e8e\u83b7\u53d6\u5143\u6570\u636e\uff0c\u68c0\u67e5\u548c\u66f4\u6539\u7c7b\u7684\u8fd0\u884c\u65f6\u884c\u4e3a\u3002\n <\/div>\n
\n java.lang\u548cjava.lang.reflect\u5305\u63d0\u4f9b\u4e86Java\u53cd\u5c04\u7684\u7c7b\u3002\n <\/div>\n

\u4f7f\u7528\u7684\u5730\u65b9<\/h3>\n
\n Reflection API\u4e3b\u8981\u7528\u4e8e:\n <\/div>\n

IDE(\u96c6\u6210\u5f00\u53d1\u73af\u5883)\uff0c\u4f8b\u5982Eclipse\uff0cMyEclipse\uff0cNetBeans\u7b49\u3002<\/span>
\n \u8c03\u8bd5\u5668<\/span>
\n \u6d4b\u8bd5\u5de5\u5177\u7b49<\/span> <\/p>\n

\n\u60a8\u77e5\u9053\u5417\uff1f <\/legend>\n

\u6709\u591a\u5c11\u79cd\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6Class\u7c7b\u7684\u5b9e\u4f8b\uff1f<\/span>
\n \u5982\u4f55\u521b\u5efajavap\u5de5\u5177\uff1f<\/span>
\n \u5982\u4f55\u521b\u5efaappletviewer\u5de5\u5177\uff1f<\/span>
\n \u5982\u4f55\u4ece\u7c7b\u5916\u90e8\u8bbf\u95ee\u79c1\u6709\u65b9\u6cd5\uff1f<\/span>
\n <\/fieldset>\n


\n

java.lang.Class\u7c7b<\/h3>\n
\n java.lang.Class\u7c7b\u4e3b\u8981\u6267\u884c\u4e24\u9879\u4efb\u52a1:\n <\/div>\n

\u63d0\u4f9b\u4e86\u5728\u8fd0\u884c\u65f6\u83b7\u53d6\u7c7b\u7684\u5143\u6570\u636e\u7684\u65b9\u6cd5\u3002<\/span>
\n \u63d0\u4f9b\u4e86\u68c0\u67e5\u548c\u66f4\u6539\u7c7b\u7684\u8fd0\u884c\u65f6\u884c\u4e3a\u7684\u65b9\u6cd5\u3002<\/span> <\/p>\n


\n
\n

Class\u7c7b\u7684\u5e38\u7528\u65b9\u6cd5: <\/h3>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
\u65b9\u6cd5<\/td>\n\u8bf4\u660e<\/td>\n<\/tr>\n
public String getName()<\/td>\n\u8fd4\u56de\u73ed\u7ea7\u540d\u79f0<\/td>\n<\/tr>\n
public static Class forName(String className)throws ClassNotFoundException <\/td>\n\u52a0\u8f7d\u7c7b\u5e76\u8fd4\u56deClass\u7c7b\u7684\u5f15\u7528\u3002<\/td>\n<\/tr>\n
public Object newInstance()throws InstantiationException,IllegalAccessException <\/td>\n\u521b\u5efa\u65b0\u5b9e\u4f8b\u3002<\/td>\n<\/tr>\n
public boolean isInterface()<\/td>\n\u68c0\u67e5\u5b83\u662f\u5426\u662f\u63a5\u53e3\u3002<\/td>\n<\/tr>\n
public boolean isArray()<\/td>\n\u68c0\u67e5\u5b83\u662f\u5426\u4e3a\u6570\u7ec4\u3002<\/td>\n<\/tr>\n
public boolean isPrimitive()<\/td>\n\u68c0\u67e5\u5b83\u662f\u5426\u539f\u59cb\u3002<\/td>\n<\/tr>\n
public Class getSuperclass()<\/td>\n\u8fd4\u56de\u8d85\u7c7b\u7684\u7c7b\u5f15\u7528\u3002<\/td>\n<\/tr>\n
public Field[] getDeclaredFields()throws SecurityException<\/td>\n\u8fd4\u56de\u6b64\u7c7b\u7684\u5b57\u6bb5\u603b\u6570\u3002<\/td>\n<\/tr>\n
public Method[] getDeclaredMethods()throws SecurityException<\/td>\n\u8fd4\u56de\u6b64\u7c7b\u7684\u65b9\u6cd5\u603b\u6570\u3002<\/td>\n<\/tr>\n
public Constructor[] getDeclaredConstructors()throws SecurityException<\/td>\n\u8fd4\u56de\u6b64\u7c7b\u7684\u6784\u9020\u51fd\u6570\u603b\u6570\u3002<\/td>\n<\/tr>\n
public Method getDeclaredMethod(String name,Class[] parameterTypes)throws NoSuchMethodException,SecurityException<\/td>\n\u8fd4\u56de\u65b9\u6cd5\u7c7b\u5b9e\u4f8b\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table><\/div>\n
\n

\u5982\u4f55\u83b7\u53d6Class\u7c7b\u7684\u5bf9\u8c61\uff1f<\/h3>\n
\n \u67093\u79cd\u83b7\u53d6Class\u7c7b\u5b9e\u4f8b\u7684\u65b9\u6cd5\u3002\u5b83\u4eec\u5982\u4e0b:\n <\/div>\n

\u7c7b\u7684
\n forName()\u65b9\u6cd5<\/span> \u5bf9\u8c61\u7c7b\u7684
\n getClass()\u65b9\u6cd5<\/span>
\n .class\u8bed\u6cd5<\/span> <\/p>\n

1)Class\u7c7b\u7684forName()\u65b9\u6cd5<\/h3>\n

\u7528\u4e8e\u52a8\u6001\u52a0\u8f7d\u7c7b\u3002<\/span>
\n \u8fd4\u56deClass\u7c7b\u7684\u5b9e\u4f8b\u3002<\/span>
\n \u5982\u679c\u60a8\u77e5\u9053\u7c7b\u7684\u5168\u9650\u5b9a\u540d\uff0c\u5219\u5e94\u4f7f\u7528\u5b83\u3002\u4e0d\u80fd\u5c06\u5176\u7528\u4e8e\u57fa\u672c\u7c7b\u578b\u3002<\/span> <\/p>\n

\n \u8ba9\u6211\u4eec\u770b\u770bforName()\u65b9\u6cd5\u7684\u7b80\u5355\u793a\u4f8b\u3002\n <\/div>\n
\n
 class <\/span>Simple{
}
class <\/span>Test{
    public <\/span>static void <\/span>main(String args[]){
        class <\/span>c=Class.forName<\/span>(\"Simple\"<\/span>);
        System.out.println<\/span>(c.getName<\/span>());
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Simple
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

2)Object class\u7684getClass()\u65b9\u6cd5<\/h3>\n
\n \u5b83\u8fd4\u56deClass\u7c7b\u7684\u5b9e\u4f8b\u3002\u5982\u679c\u60a8\u77e5\u9053\u7c7b\u578b\uff0c\u5219\u5e94\u4f7f\u7528\u5b83\u3002\u800c\u4e14\uff0c\u5b83\u53ef\u4ee5\u4e0e\u539f\u8bed\u4e00\u8d77\u4f7f\u7528\u3002\n <\/div>\n
\n
 class <\/span>Simple{
}
class <\/span>Test{
    void printName(Object obj){
        class <\/span>c=obj.getClass<\/span>();
        System.out.println<\/span>(c.getName<\/span>());
    }
    public <\/span>static void <\/span>main(String args[]){
        Simple s=new Simple();
        Test t=new Test();
        t.printName<\/span>(s);
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 Simple
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

3).class\u8bed\u6cd5<\/h3>\n
\n \u5982\u679c\u7c7b\u578b\u53ef\u7528\uff0c\u4f46\u6ca1\u6709\u5b9e\u4f8b\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u5c06\" .class\"\u9644\u52a0\u5230\u7c7b\u578b\u7684\u540d\u79f0\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u539f\u59cb\u6570\u636e\u7c7b\u578b\u3002\n <\/div>\n
\n
 class <\/span>Test{
    public <\/span>static void <\/span>main(String args[]){
        class <\/span>c = boolean.class;
        System.out.println<\/span>(c.getName<\/span>());
        class <\/span>c2 = Test.class;
        System.out.println<\/span>(c2.getName<\/span>());
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 boolean Test
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n

\u786e\u5b9a\u7c7b\u5bf9\u8c61<\/h3>\n
\n \u4f7f\u7528\u4ee5\u4e0bClass\u7c7b\u65b9\u6cd5\u786e\u5b9a\u7c7b\u5bf9\u8c61:\n <\/div>\n
\n 1)public boolean isInterface(): <\/strong>\u786e\u5b9a\u6307\u5b9a\u7684Class\u5bf9\u8c61\u662f\u5426\u8868\u793a\u63a5\u53e3\u7c7b\u578b\u3002\n <\/div>\n
\n 2)public boolean isArray(): <\/strong>\u786e\u5b9a\u6b64Class\u5bf9\u8c61\u662f\u5426\u8868\u793a\u6570\u7ec4\u7c7b\u3002\n <\/div>\n
\n 3)public boolean isPrimitive(): <\/strong>\u786e\u5b9a\u6307\u5b9a\u7684Class\u5bf9\u8c61\u662f\u5426\u8868\u793a\u539f\u59cb\u7c7b\u578b\u3002\n <\/div>\n
\n \u8ba9\u6211\u4eec\u770b\u4e00\u4e0b\u53cd\u5c04api\u786e\u5b9a\u5bf9\u8c61\u7c7b\u578b\u7684\u7b80\u5355\u793a\u4f8b\u3002\n <\/div>\n
\n
 class <\/span>Simple{
}
interface My{
}
class <\/span>Test{
    public <\/span>static void <\/span>main(String args[]){
        try{
            class <\/span>c=Class.forName<\/span>(\"Simple\"<\/span>);
            System.out.println<\/span>(c.isInterface<\/span>());
            class <\/span>c2=Class.forName<\/span>(\"My\"<\/span>);
            System.out.println<\/span>(c2.isInterface<\/span>());
        }
        catch(Exception e){
            System.out.println<\/span>(e);
        }
    }
}
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n
 false true<\/span><\/span>
<\/span><\/code><\/pre>\n<\/p><\/div>\n

Java \u62d3\u5c55\u5b66\u4e60<\/h2>\n

Java newInstance<\/span>
\n Javap\u5de5\u5177<\/span>
\n \u521b\u5efajavap\u5de5\u5177<\/span>
\n \u521b\u5efa appletviewer<\/span>
\n <\/body>
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Java \u53cd\u5c04zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-738","post","type-post","status-publish","format-standard","hentry","category-java-jichu"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/738"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=738"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/738\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}