{"id":163,"date":"2026-05-02T15:12:50","date_gmt":"2026-05-02T07:12:50","guid":{"rendered":"https:\/\/larkdrop.cafe\/?p=163"},"modified":"2026-05-02T15:12:50","modified_gmt":"2026-05-02T07:12:50","slug":"%e5%8f%8d%e5%b0%84","status":"publish","type":"post","link":"https:\/\/larkdrop.cafe\/index.php\/2026\/05\/02\/%e5%8f%8d%e5%b0%84\/","title":{"rendered":"\u53cd\u5c04"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u53cd\u5c04<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u53cd\u5c04\uff08Reflection\uff09<\/strong> \u662f\u4e00\u79cd\u5728<strong>\u8fd0\u884c\u65f6\uff08Runtime\uff09<\/strong> \u52a8\u6001\u5206\u6790\u3001\u68c0\u67e5\u548c\u64cd\u4f5c\u7c7b\u3001\u65b9\u6cd5\u3001\u5b57\u6bb5\u3001\u6784\u9020\u65b9\u6cd5\u7b49\u7a0b\u5e8f\u7ed3\u6784\u7684\u80fd\u529b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u4fd7\u6765\u8bf4\uff0c\u901a\u8fc7Java\u4e2d\u7684\u53cd\u5c04\u673a\u5236\uff0c\u53ef\u4ee5\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\uff0c\u5728\u4e0d\u77e5\u9053\u5177\u4f53\u7c7b\u6216\u8005\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u8be5\u7c7b\u7684\u5b57\u8282\u7801\u5bf9\u8c61\uff0c\u52a8\u6001\u52a0\u8f7d\u7c7b\u3001\u521b\u5efa\u5bf9\u8c61\u3001\u8c03\u7528\u65b9\u6cd5\u3001\u8bbf\u95ee\u5b57\u6bb5\uff0c\u751a\u81f3\u662f\u4fee\u6539\u79c1\u6709\u6210\u5458\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cd\u5c04\u63d0\u4f9b\u4e86\u4e00\u79cd\u5168\u65b0\u7684\u521b\u5efa\u5bf9\u8c61\uff0c\u8c03\u7528\u65b9\u6cd5\uff0c\u8bbf\u95ee\u6210\u5458\u53d8\u91cf\u7684\u5f62\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cd\u5c04\u7684\u8d77\u70b9\u662f\u5b57\u8282\u7801\u5bf9\u8c61\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u83b7\u53d6\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61\u7684\u51e0\u79cd\u65b9\u5f0f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u8c61.getClass()<\/li>\n\n\n\n<li>\u7c7b\u540d.class<\/li>\n\n\n\n<li>Class.forName(String className) \u5168\u9650\u5b9a\u540d<\/li>\n\n\n\n<li>ClassLoader\u91cc\u7684loadClass(String className)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6ce8\u610f:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u901a\u8fc7\u4ec0\u4e48\u65b9\u5f0f\u83b7\u53d6\u7684\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61 \u90fd\u662f\u540c\u4e00\u4e2a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Demo<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package _23reflect.com.cskaoyan._02cls;<br>\u200b<br>\/*<br>\u83b7\u53d6\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61\u7684\u51e0\u79cd\u65b9\u5f0f<br> *\/<br>public class Demo {<br> &nbsp; &nbsp;public static void main(String[] args) throws ClassNotFoundException {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5bf9\u8c61.getClass<br> &nbsp; &nbsp; &nbsp; &nbsp;A a = new A();<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;? extends A&gt; c1 = a.getClass();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7c7b\u540d.class<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;A&gt; c2 = A.class;<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(c1 == c2);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Class.forName(\u5168\u9650\u5b9a\u540d)<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; c3 = Class.forName(\"_23reflect.com.cskaoyan._02cls.A\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(c1 == c3);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ ClassLoader.loadClass(String className)<br> &nbsp; &nbsp; &nbsp; &nbsp;ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; c4 = systemClassLoader.loadClass(\"_23reflect.com.cskaoyan._02cls.A\");<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(c1 == c4);<br>\u200b<br> &nbsp;  }<br>}<br>\u200b<br>class A{<br>\u200b<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5173\u4e8eClass<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Class<\/code> \u7c7b\u7684\u5b9e\u4f8b\u8868\u793a\u6b63\u5728\u8fd0\u884c\u7684 Java \u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u7c7b\u548c\u63a5\u53e3<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Class<\/code> \u6ca1\u6709\u516c\u5171\u6784\u9020\u65b9\u6cd5\u3002<code>Class<\/code> \u5bf9\u8c61\u662f\u5728\u52a0\u8f7d\u7c7b\u65f6\u7531 Java \u865a\u62df\u673a\u4ee5\u53ca\u901a\u8fc7\u8c03\u7528\u7c7b\u52a0\u8f7d\u5668\u4e2d\u7684 <code>defineClass<\/code> \u65b9\u6cd5\u81ea\u52a8\u6784\u9020\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6784\u9020\u65b9\u6cd5(Constructor)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6240\u6709\u6784\u9020\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u83b7\u53d6\u6240\u6709\u7684public\u4fee\u9970\u7684\u6784\u9020\u65b9\u6cd5<br>Constructor[] getConstructors()<br> &nbsp; &nbsp;<br> &nbsp; &nbsp;<br>\/\/ \u83b7\u53d6\u6240\u6709\u7684\u6784\u9020\u65b9\u6cd5\uff0c\u5305\u62ec\u79c1\u6709\u6784\u9020\u65b9\u6cd5<br>Constructor[] getDeclaredConstructors()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u83b7\u53d6\u6307\u5b9a\u6784\u9020\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Constructor&lt;T&gt; getConstructor(Class&lt;?&gt;... parameterTypes)<br>Constructor&lt;T&gt; getDeclaredConstructor(Class&lt;?&gt;... parameterTypes)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f7f\u7528Constructor\u521b\u5efa\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Person p = new Person(\"zs\",20,true)<br>newInstance(\u53c2\u6570\u5217\u8868)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u66b4\u529b\u7834\u89e3<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">setAccessible(true) <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">package _23reflect.com.cskaoyan._04api;<br>\u200b<br>import java.lang.reflect.Constructor;<br>import java.lang.reflect.InvocationTargetException;<br>\u200b<br>\u200b<br>\u200b<br>public class ConstructorTest {<br> &nbsp; &nbsp;public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u53cd\u5c04\u6280\u672f\u7684\u8d77\u70b9 \u83b7\u53d6\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; stuCls = Class.forName(\"_23reflect.com.cskaoyan.bean.Person\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6240\u6709\u7684public\u7684\u6784\u9020\u65b9\u6cd5----\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Constructor[] getConstructors()<br> &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;?&gt;[] constructors = stuCls.getConstructors();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Constructor&lt;?&gt; constructor : constructors) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(constructor);<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6240\u6709\u7684\u6784\u9020\u65b9\u6cd5----\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Constructor[] getDeclaredConstructors()<br> &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;?&gt;[] declaredConstructors = stuCls.getDeclaredConstructors();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Constructor&lt;?&gt; constructor : declaredConstructors) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(constructor);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6307\u5b9a\u7684public\u7684\u6784\u9020\u65b9\u6cd5----\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Constructor&lt;T&gt; getConstructor(Class&lt;?&gt;... parameterTypes)<br> &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;?&gt; constructor = stuCls.getConstructor(String.class, int.class, boolean.class);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Constructor&lt;?&gt; constructor = stuCls.getConstructor(String.class, int.class);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ java.lang.NoSuchMethodException<br>\u200b<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(constructor);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6307\u5b9a\u7684\u6784\u9020\u65b9\u6cd5----\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Constructor&lt;T&gt; getDeclaredConstructor(Class&lt;?&gt;... parameterTypes)<br> &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;?&gt; declaredConstructor = stuCls.getDeclaredConstructor(String.class, int.class);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(declaredConstructor);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u4f7f\u7528\u6784\u9020\u65b9\u6cd5\u5bf9\u8c61\u521b\u5efa\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ newInstance<br> &nbsp; &nbsp; &nbsp; &nbsp;Object o = constructor.newInstance(\"zs\", 20, true);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(o);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ java.lang.IllegalAccessException<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ setAccessible(true)  \u5ffd\u7565java\u8bed\u6cd5\u68c0\u67e5<br> &nbsp; &nbsp; &nbsp; &nbsp;declaredConstructor.setAccessible(true);<br> &nbsp; &nbsp; &nbsp; &nbsp;Object o1 = declaredConstructor.newInstance(\"ls\", 21);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(o1);<br>\u200b<br>\u200b<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6210\u5458\u53d8\u91cf(Field)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6240\u6709\u6210\u5458\u53d8\u91cf<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u83b7\u53d6\u6240\u6709public \u4fee\u9970\u7684\u6210\u5458\u53d8\u91cf,\u5305\u62ec\u7ee7\u627f\u81ea\u7236\u7c7b\u4e2d\u7684\u6210\u5458<br>Field[] getFields()<br> &nbsp; &nbsp;<br>\/\/ \u83b7\u53d6\u6240\u6709\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\uff0c\u5305\u62ec\u79c1\u6709\u6210\u5458 &nbsp; &nbsp;<br>Field[] getDeclaredFields()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u83b7\u53d6\u6307\u5b9a\u6210\u5458\u53d8\u91cf<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Field getField(String name)<br>Field getDeclaredField(String name)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u901a\u8fc7Field\u8bfb\u5199\u5bf9\u8c61\u7684\u6210\u5458\u53d8\u91cf(\u53ef\u66b4\u529b\u7834\u89e3)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Object get(Object obj)\uff1a\u83b7\u53d6\u503c\uff0c\u4f20\u5165\u5bf9\u8c61<br>void set(Object obj, Object value)\uff1a\u8d4b\u503c\uff0c\u4f20\u5165\u5bf9\u8c61<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">package _23reflect.com.cskaoyan._04api;<br>\u200b<br>import _23reflect.com.cskaoyan.bean.Person;<br>\u200b<br>import java.lang.reflect.Constructor;<br>import java.lang.reflect.Field;<br>\u200b<br>\/*<br>\u53cd\u5c04\u83b7\u53d6\u6210\u5458\u53d8\u91cf<br>\u200b<br> *\/<br>public class FieldTest {<br> &nbsp; &nbsp;public static void main(String[] args) throws Exception{<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; personCls = Class.forName(\"_23reflect.com.cskaoyan.bean.Person\");<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6240\u6709\u7684public\u7684\u6210\u5458\u53d8\u91cf------\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Field[] getFields()<br> &nbsp; &nbsp; &nbsp; &nbsp;Field[] fields = personCls.getFields();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Field field : fields) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(field);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6240\u6709\u7684\u6210\u5458\u53d8\u91cf------\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Field[] getDeclaredFields()<br> &nbsp; &nbsp; &nbsp; &nbsp;Field[] declaredFields = personCls.getDeclaredFields();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Field field : declaredFields) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(field);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6307\u5b9a\u7684public\u7684\u6210\u5458\u53d8\u91cf------\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Field getField(String name)<br> &nbsp; &nbsp; &nbsp; &nbsp;Field nameField = personCls.getField(\"name\");<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(nameField);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6307\u5b9a\u7684\u6210\u5458\u53d8\u91cf------\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Field getDeclaredField(String name)<br> &nbsp; &nbsp; &nbsp; &nbsp;Field ageField = personCls.getDeclaredField(\"age\");<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(ageField);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u7ed9\u6210\u5458\u53d8\u91cf\u8d4b\u503c \u83b7\u53d6\u6210\u5458\u53d8\u91cf\u7684\u503c<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/void set(Object obj, Object value)\uff1a\u8d4b\u503c\uff0c\u4f20\u5165\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;?&gt; declaredConstructor = personCls.getDeclaredConstructor();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u5b9e\u4f8b\u5316\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Object o = declaredConstructor.newInstance();<br> &nbsp; &nbsp; &nbsp; &nbsp;nameField.set(o, \"zs\");<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(o);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;ageField.setAccessible(true);<br> &nbsp; &nbsp; &nbsp; &nbsp;ageField.set(o, 22);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(o);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Object get(Object obj)\uff1a\u83b7\u53d6\u503c\uff0c\u4f20\u5165\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Object o1 = nameField.get(o);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(o1);<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u6210\u5458\u65b9\u6cd5(Method)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u83b7\u53d6\u6240\u6709\u6210\u5458\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ \u83b7\u53d6\u6240\u6709\u7684public\u4fee\u9970\u7684\u65b9\u6cd5,\u5305\u62ec\u7ee7\u627f\u7684\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5<br>Method[] getMethods();<br>\u200b<br>\/\/ \u83b7\u53d6\u6240\u6709\u5728\u8be5\u7c7b\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u79c1\u6709\u65b9\u6cd5\uff0c\u4e0d\u5305\u62ec\u7236\u7c7b\u4e2d\u7684\u65b9\u6cd5<br>Method[] getDeclaredMethods()<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u83b7\u53d6\u6307\u5b9a\u7684\u6210\u5458\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Method getMethod(String name, Class&lt;?&gt;... parameterTypes)<br>Method getDeclaredMethod(String name, Class&lt;?&gt;... parameterTypes)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5229\u7528Method\u8c03\u7528\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Object invoke(Object obj, Object... args)<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">package _23reflect.com.cskaoyan._04api;<br>\u200b<br>import _23reflect.com.cskaoyan.bean.Person;<br>\u200b<br>import java.lang.reflect.Constructor;<br>import java.lang.reflect.Method;<br>\u200b<br>\/**<br> * @description:<br> * @author: \u666f\u5929<br> * @date: 2022\/10\/24 14:39<br> **\/<br>\/*<br>\u901a\u8fc7\u53cd\u5c04\u83b7\u53d6\u65b9\u6cd5<br> *\/<br>public class MethodTest {<br> &nbsp; &nbsp;public static void main(String[] args) throws Exception{<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; personCls = Class.forName(\"_23reflect.com.cskaoyan.bean.Person\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6240\u6709\u7684public\u7684\u65b9\u6cd5----\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Method[] getMethods()<br> &nbsp; &nbsp; &nbsp; &nbsp;Method[] methods = personCls.getMethods();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Method method : methods) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(method);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6240\u6709\u7684\u65b9\u6cd5----\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Method[] getDeclaredMethods()<br> &nbsp; &nbsp; &nbsp; &nbsp;Method[] declaredMethods = personCls.getDeclaredMethods();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Method method : declaredMethods) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(method);<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6307\u5b9a\u7684public\u7684\u65b9\u6cd5----\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Method getMethod(String name, Class&lt;?&gt;... parameterTypes)<br> &nbsp; &nbsp; &nbsp; &nbsp;Method eatMethod1 = personCls.getMethod(\"eat\");<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(eatMethod1);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"\u83b7\u53d6\u6307\u5b9a\u7684\u65b9\u6cd5----\");<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Method getDeclaredMethod(String name, Class&lt;?&gt;... parameterTypes)<br> &nbsp; &nbsp; &nbsp; &nbsp;Method eatMethod2 = personCls.getDeclaredMethod(\"eat\", String.class);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(eatMethod2);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Person p = new Person();<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/p.eat();<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u53cd\u5c04\u8c03\u7528\u65b9\u6cd5<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ Object invoke(Object obj, Object... args)<br> &nbsp; &nbsp; &nbsp; &nbsp;Constructor&lt;?&gt; declaredConstructor = personCls.getDeclaredConstructor();<br> &nbsp; &nbsp; &nbsp; &nbsp;Object o = declaredConstructor.newInstance();<br> &nbsp; &nbsp; &nbsp; &nbsp;Object invoke = eatMethod1.invoke(o);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(invoke);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;eatMethod2.setAccessible(true);<br> &nbsp; &nbsp; &nbsp; &nbsp;eatMethod2.invoke(o, \"apple\");<br>\u200b<br> &nbsp;  }<br>}<br>\u200b<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u8865\u5145<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u4ed6API<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">package _23reflect.com.cskaoyan.bean;<br>\u200b<br>\/**<br> * @description:<br> * @author: \u666f\u5929<br> * @date: 2022\/10\/24 11:31<br> **\/<br>\u200b<br>public class Person {<br> &nbsp; &nbsp;\/\/ \u5b9a\u4e49\u6210\u5458\u53d8\u91cf<br> &nbsp; &nbsp;public &nbsp;String name;<br> &nbsp; &nbsp;private int age;<br> &nbsp; &nbsp;boolean gender;<br>\u200b<br> &nbsp; &nbsp;\/\/ \u6784\u9020\u65b9\u6cd5<br>\u200b<br> &nbsp; &nbsp;public Person(String name, int age, boolean gender) {<br> &nbsp; &nbsp; &nbsp; &nbsp;this.name = name;<br> &nbsp; &nbsp; &nbsp; &nbsp;this.age = age;<br> &nbsp; &nbsp; &nbsp; &nbsp;this.gender = gender;<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;private Person(String name, int age) {<br> &nbsp; &nbsp; &nbsp; &nbsp;this.name = name;<br> &nbsp; &nbsp; &nbsp; &nbsp;this.age = age;<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;public Person() {<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;\/\/ \u5b9a\u4e49\u6210\u5458\u65b9\u6cd5<br>\u200b<br> &nbsp; &nbsp;@Override<br> &nbsp; &nbsp;public String toString() {<br> &nbsp; &nbsp; &nbsp; &nbsp;return \"Person{\" +<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"name='\" + name + '\\'' +<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\", age=\" + age +<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\", gender=\" + gender +<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'}';<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;public String eat(){<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"eat food!\");<br> &nbsp; &nbsp; &nbsp; &nbsp;return \"\u5403\u4e86\";<br> &nbsp;  }<br>\u200b<br> &nbsp; &nbsp;private void eat(String food){<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"eat \" + food);<br> &nbsp;  }<br>}<br>\u200b<br>\u200b<br>import java.lang.reflect.Field;<br>import java.lang.reflect.Method;<br>import java.lang.reflect.Modifier;<br>\u200b<br>\u200b<br>public class OtherApi {<br> &nbsp; &nbsp;public static void main(String[] args) throws Exception{<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u5b57\u8282\u7801\u6587\u4ef6\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; c = Class.forName(\"_23reflect.com.cskaoyan.bean.Person\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/Class&lt;?&gt; c = Class.forName(\"java.io.OutputStream\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u5168\u9650\u5b9a\u7c7b\u540d<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(c.getName());<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u7b80\u5355\u540d\u79f0<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"c.getSimpleName() = \" + c.getSimpleName());<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u7236\u7c7b<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; superclass = c.getSuperclass();<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"superclass = \" + superclass.getSimpleName());<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u5b9e\u73b0\u7684\u63a5\u53e3<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt;[] interfaces = c.getInterfaces();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Class&lt;?&gt; i : interfaces) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(i);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u7c7b\u52a0\u8f7d\u5668<br> &nbsp; &nbsp; &nbsp; &nbsp;ClassLoader classLoader = c.getClassLoader();<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"classLoader = \" + classLoader);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6name\u8fd9\u4e2a\u6210\u5458\u53d8\u91cf\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Field nameField = c.getDeclaredField(\"name\");<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6\u6743\u9650\u4fee\u9970\u7b26<br> &nbsp; &nbsp; &nbsp; &nbsp;int modifiers = nameField.getModifiers();<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(modifiers);<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ static String toString(int mod)<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u8fd4\u56de\u63cf\u8ff0\u6307\u5b9a\u4fee\u9970\u7b26\u4e2d\u7684\u8bbf\u95ee\u4fee\u9970\u7b26\u6807\u5fd7\u7684\u5b57\u7b26\u4e32\u3002<br> &nbsp; &nbsp; &nbsp; &nbsp;String s = Modifier.toString(modifiers);<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"s = \" + s);<br>\u200b<br> &nbsp; &nbsp; &nbsp; &nbsp;\/\/ \u83b7\u53d6eat(String s)\u65b9\u6cd5\u5bf9\u8c61<br> &nbsp; &nbsp; &nbsp; &nbsp;Method eatMethod = c.getDeclaredMethod(\"eat\", String.class);<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt; returnType = eatMethod.getReturnType();<br> &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(\"returnType = \" + returnType);<br> &nbsp; &nbsp; &nbsp; &nbsp;Class&lt;?&gt;[] parameterTypes = eatMethod.getParameterTypes();<br> &nbsp; &nbsp; &nbsp; &nbsp;for (Class&lt;?&gt; parameterType : parameterTypes) {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println(parameterType.getSimpleName());<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<br>}<br>\u200b<br>\u200b<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u53cd\u5c04 \u53cd\u5c04\uff08Reflection\uff09 \u662f\u4e00\u79cd\u5728\u8fd0\u884c\u65f6\uff08Runtime\uff09 \u52a8\u6001\u5206\u6790\u3001\u68c0\u67e5\u548c\u64cd\u4f5c\u7c7b\u3001\u65b9\u6cd5\u3001\u5b57\u6bb5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[12],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-learn","tag-java"],"_links":{"self":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":2,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/posts\/163\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/larkdrop.cafe\/index.php\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}