相关文章推荐
年轻有为的滑板  ·  java ...·  13 小时前    · 
俊逸的匕首  ·  java 消息模板替换 - CSDN文库·  13 小时前    · 
瘦瘦的伤痕  ·  [mongodb] ...·  5 小时前    · 
慈祥的饺子  ·  linux定时任务 ...·  1 月前    · 
重情义的电池  ·  C# ...·  2 月前    · 
沉稳的电梯  ·  qt udb socket ...·  3 月前    · 

Java Class getMethod() Method

The getGenericSuperClass() method of java Class class returns a method object representing the specified public member method of the class or interface represented by this Class object. The name parameter is passed as a string.

Syntax

public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException

Parameter

name - the name of the method

parameterTypes - list of parameters

Returns

The Method object.

Throws

NoSuchMethodException , NullPointerException , SecurityException

Example 1

import java.lang.reflect.*; public class ClassgetMethodExample1 { public static void main(String[] args) { ClassgetMethodExample1 class1 = new ClassgetMethodExample1(); Class cls = class1.getClass(); try { Method mthd = cls.getMethod("showMethod", null); System.out.println("method = " + mthd.toString()); } catch(NoSuchMethodException e) { System.out.println(e.toString()); //print exception object try { Class[] aarg = new Class[1]; aarg[0] = Long.class; Method lmthd = cls.getMethod("showLongMethod", aarg); System.out.println("method = " + lmthd.toString()); } catch(NoSuchMethodException e) { System.out.println(e.toString()); //print exception object public Integer showMethod() { return 1; public void showLongMethod(Long lng) { this.lng = lng; long lng = 788995; Test it Now

Output:

method = public java.lang.Integer ClassgetMethodExample1.showMethod()
method = public void ClassgetMethodExample1.showLongMethod(java.lang.Long)

Example 2

//import statements? import java.lang.reflect.Method; public class ClassgetMethodExample2 { public static void main(String... args) throws NoSuchMethodException { Class<ClassgetMethodExample2> class1 = ClassgetMethodExample2.class; Method mthd = class1.getMethod("IntCalc", int.class); System.out.println(mthd); mthd = class1.getMethod("Work"); System.out.println(mthd); mthd = class1.getMethod("StaticMethod", String.class); System.out.println(mthd); mthd = class1.getMethod("Work"); System.out.println(mthd); public int IntCalc(int i) {return 0;} public void Work() {} public static void StaticMethod(String s) {} Test it Now

Output:

public int ClassgetMethodExample2.IntCalc(int)
public void ClassgetMethodExample2.Work()
public static void ClassgetMethodExample2.StaticMethod(java.lang.String)
public void ClassgetMethodExample2.Work()
Next TopicJava Class