{"id":36031,"date":"2024-11-26T11:09:17","date_gmt":"2024-11-26T03:09:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/36031\/"},"modified":"2024-11-26T11:09:17","modified_gmt":"2024-11-26T03:09:17","slug":"java-%e5%8f%8d%e5%b0%84%e4%b8%ad-nosuchmethodexception-%e5%bc%82%e5%b8%b8%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e6%98%8e%e6%98%8e%e5%ad%98%e5%9c%a8%e6%96%b9%e6%b3%95%e5%8d%b4%e6%8a%9b%e5%87%ba%e5%bc%82","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/36031\/","title":{"rendered":"Java \u53cd\u5c04\u4e2d NoSuchMethodException \u5f02\u5e38\uff1a\u4e3a\u4ec0\u4e48\u660e\u660e\u5b58\u5728\u65b9\u6cd5\u5374\u629b\u51fa\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173088254828531.jpg\" class=\"aligncenter\" title=\"Java \u53cd\u5c04\u4e2d NoSuchMethodException \u5f02\u5e38\uff1a\u4e3a\u4ec0\u4e48\u660e\u660e\u5b58\u5728\u65b9\u6cd5\u5374\u629b\u51fa\u5f02\u5e38\uff1f\u63d2\u56fe\" alt=\"Java \u53cd\u5c04\u4e2d NoSuchMethodException \u5f02\u5e38\uff1a\u4e3a\u4ec0\u4e48\u660e\u660e\u5b58\u5728\u65b9\u6cd5\u5374\u629b\u51fa\u5f02\u5e38\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u53cd\u5c04\u4e2d\u7684 nosuchmethodexception<\/strong><\/p>\n<p>\u672c\u6587\u5c06\u63a2\u8ba8\u5728\u4f7f\u7528 java \u53cd\u5c04\u65f6\u78b0\u5230\u7684 nosuchmethodexception \u95ee\u9898\u3002 <\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u5982\u95ee\u9898\u6240\u8ff0\uff0c\u5f53\u5c1d\u8bd5\u4f7f\u7528getmethod() \u83b7\u53d6com.demo.pojo.user \u7c7b\u7684getname \u65b9\u6cd5\u65f6\uff0c\u4f1a\u629b\u51fanosuchmethodexception \u5f02\u5e38\u3002\u800c\u5b9e\u9645\u4e0a\uff0c\u8be5\u7c7b\u7684\u786e\u5b58\u5728 getname \u65b9\u6cd5\u3002 <\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>nosuchmethodexception \u901a\u5e38\u8868\u793a\u5728\u6307\u5b9a\u7c7b\u4e2d\u627e\u4e0d\u5230\u4e0e\u6307\u5b9a\u53c2\u6570\u5b57\u7b7e\u540d\u5339\u914d\u7684\u65b9\u6cd5\u3002\u95ee\u9898\u4e2d\u7684\u4ee3\u7801\u4e2d\uff0cgetname \u65b9\u6cd5\u88ab\u8c03\u7528\u4e86\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u5b9e\u9645\u4e0a\u8be5\u65b9\u6cd5\u6ca1\u6709\u53c2\u6570\u3002 <\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8bf7\u8c03\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>\/\/getname\u65b9\u6cd5\u6c92\u6709\u53c3\u6578\u54e6\uff0c\u53ef\u8b8a\u53c3\u6578\u5217\u8868\u4e0d\u50b3\nmethod method = c.getmethod(\"getname\");<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u6700\u7ec8\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>public class test {\n    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {\n        Class c = Class.forName(\"com.demo.pojo.user\");\n        Object obj = c.newInstance();\n        Method[] methods = c.getMethods();\n        for (Method method: methods) {\n            System.out.println(method);\n        }\n        \/\/ getName\u65b9\u6cd5\u6c92\u6709\u53c3\u6578\u54e6\uff0c\u53ef\u8b8a\u53c3\u6578\u5217\u8868\u4e0d\u50b3\n        Method method = c.getMethod(\"getName\");\n        \/\/ \u6b64\u8655\u540c\u4e0a\n        System.out.println(method.invoke(obj));\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7ecf\u8fc7\u4fee\u6539\u540e\uff0c\u4ee3\u7801\u5c31\u80fd\u6b63\u786e\u83b7\u53d6\u548c\u8c03\u7528 getname \u65b9\u6cd5\u4e86\u3002\u53e6\u5916\u8bf7\u6ce8\u610f\uff0c\u7c7b\u540d\u5e94\u5927\u5199\uff0c\u4ee5\u7b26\u5408 java \u89c4\u8303\u3002 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fJava \u53cd\u5c04\u4e2d NoSuchMethodException \u5f02\u5e38\uff1a\u4e3a\u4ec0\u4e48\u660e\u660e\u5b58\u5728\u65b9\u6cd5\u5374\u629b\u51fa\u5f02\u5e38\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53cd\u5c04\u4e2d\u7684 nosuchmethodexception \u672c\u6587\u5c06\u63a2\u8ba8\u5728\u4f7f\u7528 java \u53cd\u5c04\u65f6\u78b0\u5230\u7684 nosuchmethodexception \u95ee\u9898\u3002 \u95ee\u9898\u63cf\u8ff0 \u5982\u95ee\u9898\u6240\u8ff0\uff0c\u5f53\u5c1d\u8bd5\u4f7f\u7528getmethod() \u83b7\u53d6com.demo.pojo.user \u7c7b\u7684getname \u65b9\u6cd5\u65f6\uff0c\u4f1a\u629b\u51fanosuchmethodexception \u5f02\u5e38\u3002\u800c\u5b9e\u9645\u4e0a\uff0c\u8be5\u7c7b\u7684\u786e\u5b58\u5728 getname \u65b9\u6cd5\u3002 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u89e3\u51b3\u65b9\u6848 nosuchmethodexception \u901a\u5e38\u8868\u793a\u5728\u6307\u5b9a\u7c7b\u4e2d\u627e\u4e0d\u5230\u4e0e\u6307\u5b9a\u53c2\u6570\u5b57\u7b7e\u540d\u5339\u914d\u7684\u65b9\u6cd5\u3002\u95ee\u9898\u4e2d\u7684\u4ee3\u7801\u4e2d\uff0cgetname \u65b9\u6cd5\u88ab\u8c03\u7528\u4e86\u4e24\u4e2a\u53c2\u6570\uff0c\u4f46\u5b9e\u9645\u4e0a\u8be5\u65b9\u6cd5\u6ca1\u6709\u53c2\u6570\u3002 \u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u8bf7\u8c03\u6574\u4ee3\u7801\u5982\u4e0b\uff1a \/\/getname\u65b9\u6cd5\u6c92\u6709\u53c3\u6578\u54e6\uff0c\u53ef\u8b8a\u53c3\u6578\u5217\u8868\u4e0d\u50b3 method method = c.getmethod(&#8220;getname&#8221;); \u767b\u5f55\u540e\u590d\u5236 \u6700\u7ec8\u4ee3\u7801\u5982\u4e0b\uff1a public class test { public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException { Class c = Class.forName(&#8220;com.demo.pojo.user&#8221;); Object obj = c.newInstance(); Method[] methods = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-36031","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=36031"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/36031\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=36031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=36031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=36031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}