
{"id":2259,"date":"2020-09-08T15:48:48","date_gmt":"2020-09-08T07:48:48","guid":{"rendered":"http:\/\/127.0.0.2\/?p=2259"},"modified":"2020-09-10T08:24:34","modified_gmt":"2020-09-10T00:24:34","slug":"2020java%e6%95%99%e7%a8%8b%ef%bc%9a%e5%8f%8d%e5%b0%84","status":"publish","type":"post","link":"https:\/\/www.9713job.com\/?p=2259","title":{"rendered":"2020java\u6559\u7a0b\uff1a\u53cd\u5c04"},"content":{"rendered":"<h3>\u53cd\u5c04<\/h3>\n<p><strong>\u4ec0\u4e48\u662f\u7c7b\u5bf9\u8c61\uff1f<\/strong><br \/>\n\u7c7b\u7684\u5bf9\u8c61\uff1a\u57fa\u4e8e\u67d0\u4e2a\u7c7b new \u51fa\u6765\u7684\u5bf9\u8c61\uff0c\u4e5f\u6210\u4e3a\u5b9e\u4f8b\u5bf9\u8c61\u3002<br \/>\n\u7c7b\u5bf9\u8c61\uff1a\u7c7b\u52a0\u8f7d\u7684\u4ea7\u7269\uff0c\u5c01\u88c5\u4e86\u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u4fe1\u606f(\u7c7b\u540d\uff0c\u7236\u7c7b\uff0c\u63a5\u53e3\uff0c\u5c5e\u6027\uff0c\u65b9\u6cd5\uff0c\u6784\u9020\u65b9\u6cd5)\u3002<\/p>\n<p><strong>\u83b7\u53d6\u7c7b\u5bf9\u8c61<\/strong><\/p>\n<p>\u901a\u8fc7\u7c7b\u7684\u5bf9\u8c61\uff0c\u83b7\u53d6\u7c7b\u5bf9\u8c61<br \/>\nStudent s=new Student();<br \/>\nClass c=s.getClass();<\/p>\n<p>\u901a\u8fc7\u7c7b\u540d\u83b7\u53d6\u7c7b\u5bf9\u8c61<br \/>\nClass c=\u7c7b\u540d.class<\/p>\n<p>\u901a\u8fc7\u9759\u6001\u65b9\u6cd5\u83b7\u53d6\u7c7b\u5bf9\u8c61<br \/>\nClass c=Class.forName(&#8220;\u5305\u540d.\u7c7b\u540d&#8221;)<!--more--><\/p>\n<p><strong>\u5e38\u7528\u65b9\u6cd5<\/strong><br \/>\npublic String getName()<br \/>\npublic Package getPackage()<br \/>\npublic Class&lt;? super T&gt; getSuperclass()<br \/>\npublic Class&lt;?&gt;[] getInterfaces()<br \/>\npublic field[] getFields()<br \/>\npublic Method[] getMethods()<br \/>\npublic Constructor&lt;?&gt;[] getConstructors()<br \/>\npublic T newInstance()<\/p>\n<p><strong>\u5de5\u5382\u8bbe\u8ba1\u6a21\u5f0f<\/strong><br \/>\n\u5f00\u53d1\u4e2d\u6709\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u539f\u5219\u201c\u5f00\u95ed\u539f\u5219\u201d\uff0c\u5bf9\u62d3\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5173\u95ed\u3002<br \/>\n\u5de5\u5382\u6a21\u5f0f\u4e3b\u8981\u8d1f\u8d23\u5bf9\u8c61\u521b\u5efa\u7684\u95ee\u9898\u3002<br \/>\n\u53ef\u901a\u8fc7\u53cd\u5c04\u8fdb\u884c\u5de5\u5382\u6a21\u5f0f\u7684\u8bbe\u8ba1\uff0c\u5b8c\u6210\u52a8\u6001\u7684\u5bf9\u8c61\u521b\u5efa\u3002<\/p>\n<p><span style=\"color: #ff0000;\"><strong>\u5de5\u5382\u6a21\u5f0f\u83b7\u53d6\u7c7b\u5bf9\u8c61<\/strong><\/span><\/p>\n<pre>import java.io.*;\r\n\r\npublic class demos{\r\n    public static void main(String[] args) {\r\n        BufferedReader br=null;\r\n        try{\r\n            br=new BufferedReader(new FileReader(new File(\"app\\\\config.txt\")));\r\n            String name = br.readLine();\r\n            Object o=createClass(name);\r\n            System.out.println(o);\r\n        }catch (Exception e){}\r\n    }\r\n    public static Object createClass(String str){\r\n        Class c=null;\r\n        try{\r\n            c=Class.forName(str);\r\n            return c.newInstance();\r\n        }catch (Exception e){}\r\n        return null;\r\n    }\r\n}\r\nclass Teacher{}<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>\u901a\u8fc7\u53cd\u5c04\u5b9e\u73b0\u901a\u7528\u7f16\u7a0b(\u8c03\u7528\u4efb\u4f55\u65b9\u6cd5)<\/strong><\/span><\/p>\n<pre>import java.lang.reflect.Method;\r\nimport java.util.Properties;\r\n\r\npublic class demos{\r\n    public static void main(String[] args) {\r\n        Properties properties=new Properties();\r\n        invokeAny(properties,\"setProperty\",new Class[]{String.class,String.class},\"CN\",\"\u4e2d\u56fd\");\r\n        System.out.println(properties.getProperty(\"CN\"));\r\n    }\r\n    public static void invokeAny(Object obj,String methodName,Class[] types,Object... o){\r\n        Class c=null;\r\n        try{\r\n            c=obj.getClass();\r\n            Method method = c.getDeclaredMethod(methodName, types);\r\n            method.invoke(obj,o);\r\n        }catch (Exception e){}\r\n    }\r\n}<\/pre>\n<pre>import java.lang.reflect.Method;\r\n\r\npublic class demos{\r\n    public static void main(String[] args) {\r\n        invokeAny(new Teacher(),\"tech\",new Class[]{String.class,int.class},\"tom\",22);\r\n    }\r\n    public static void invokeAny(Object obj,String methodName,Class[] types,Object... args){\r\n        Class c=null;\r\n        try{\r\n            c=obj.getClass();\r\n            Method m = c.getDeclaredMethod(methodName, types);\r\n            m.setAccessible(true);\/\/\u6ce8\u610f\uff1a\u53cd\u5c04\u4e00\u79cdjava\u7684\u5e95\u5c42\u6280\u672f\uff0c\u53ef\u4ee5\u65e0\u89c6\u5c01\u88c5\r\n            m.invoke(obj,args);\r\n        }catch (Exception e){}\r\n    }\r\n}\r\nclass Teacher{\r\n    private String name;\r\n    private Integer age;\r\n    private Double salary;\r\n    public Teacher() {\r\n    }\r\n    public Teacher(String name, Integer age, Double salary) {\r\n        this.name = name;\r\n        this.age = age;\r\n        this.salary = salary;\r\n    }\r\n    public void tech(String name,int age){\r\n        System.out.println(name+age);\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53cd\u5c04 \u4ec0\u4e48\u662f\u7c7b\u5bf9\u8c61\uff1f \u7c7b\u7684\u5bf9\u8c61\uff1a\u57fa\u4e8e\u67d0\u4e2a\u7c7b new \u51fa\u6765\u7684\u5bf9\u8c61\uff0c\u4e5f\u6210\u4e3a\u5b9e\u4f8b\u5bf9\u8c61\u3002 \u7c7b\u5bf9\u8c61\uff1a\u7c7b\u52a0\u8f7d\u7684\u4ea7\u7269\uff0c\u5c01\u88c5\u4e86 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2259","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2259"}],"version-history":[{"count":8,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2259\/revisions"}],"predecessor-version":[{"id":2267,"href":"https:\/\/www.9713job.com\/index.php?rest_route=\/wp\/v2\/posts\/2259\/revisions\/2267"}],"wp:attachment":[{"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.9713job.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}