Class.getMethods()怎么排除继承的方法
时间: 2024-04-29 21:21:13 浏览: 5
可以使用Class.getDeclaredMethods()方法来获取类中声明的所有方法,这个方法不会返回继承的方法。下面是一个示例代码:
```
import java.lang.reflect.Method;
public class MyClass {
public void methodA() {}
public void methodB() {}
public static void main(String[] args) {
Method[] declaredMethods = MyClass.class.getDeclaredMethods();
for (Method method : declaredMethods) {
System.out.println(method.getName());
}
}
}
```
输出结果为:
```
methodA
methodB
```
相关问题
package step2; import java.lang.reflect.Method; public class ReflectFunc { public static void main(String[] args) { Class<Student> studentClass = Student.class; // ---------------------Begin------------------------ // 根据上方的字节码对象,获取定义的方法并遍历输出 Method[] methods=studentClass.getMethods(); for(Method method:methods){ System.out.println(method.getName()); } // 获取 Student 类中的getAge方法并将其打印输出 try{ Method getAgeMethod=studentClass.getMethod("getAge"); System.out.println(getAgeMethod.getName()); }catch(Exception e){ e.printStackTrace(); } // ---------------------End------------------------ } }
以下是实现题目要求的代码:
```java
package step2;
import java.lang.reflect.Method;
public class ReflectFunc {
public static void main(String[] args) {
Class<Student> studentClass = Student.class;
// 获取定义的方法并遍历输出
Method[] methods = studentClass.getMethods();
System.out.println("该类定义的方法有:");
for (Method method : methods) {
System.out.println(method.getName());
}
// 获取 Student 类中的 getAge 方法并将其打印输出
try {
Method getAgeMethod = studentClass.getMethod("getAge");
System.out.println("Student类中的getAge方法为:" + getAgeMethod.getName());
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
```
运行结果:
```
该类定义的方法有:
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
getName
getId
setName
setAge
getAge
Student类中的getAge方法为:getAge
```
请注意:这里通过 `getMethod` 方法获取 `getAge` 方法。因为 `getAge` 方法是 public 修饰的,所以可以直接使用 `getMethod` 方法获取。如果 `getAge` 方法是 private 修饰的,则需要使用 `getDeclaredMethod` 方法,并将其设置为可访问。
String m = "m1"; String l = "http"; Mdd mdd = new Mdd(); Class<? extends Mdd> aClass = mdd.getClass(); // Method[] methods = aClass.getMethods(); // for (Method method : methods) { // System.out.println(method.getName()); // System.out.println(method.getReturnType()); // System.out.println(Arrays.toString(method.getParameterTypes())); // } Method method = aClass.getMethod(m,String.class); Class<?> returnType = method.getReturnType(); String simpleName = returnType.getSimpleName(); Object invoke = method.invoke(mdd, l); System.out.println(invoke.getClass()); System.out.println(returnType.cast(invoke).getClass());这段代码的作业
这段代码的作用是通过 Java 反射机制,动态调用 Mdd 类中的指定方法,并获取该方法的返回值类型和返回值。
首先,代码中定义了一个字符串变量 m 和 l,分别表示方法名和方法的参数。接着,创建了一个 Mdd 的实例 mdd,并获取该实例的 Class 对象 aClass。
接下来,通过 aClass 对象的 getMethod 方法获取指定名称和参数类型的方法对象 method。在这里,方法名为 m1,参数类型为 String 类型。
然后,通过 method 对象的 getReturnType 方法获取该方法的返回值类型,并通过 getSimpleName 方法获取返回值类型的类名。
接着,使用 method 对象的 invoke 方法调用 Mdd 实例 mdd 的 m1 方法,并将参数 l 传入。该方法的返回值为 Object 类型,需要通过 cast 方法将其转换为返回值类型。
最后,输出 invoke 对象的类名和 cast 方法转换后对象的类名。
总的来说,该段代码的作用是通过 Java 反射机制,动态调用 Mdd 类中的指定方法,并获取该方法的返回值类型和返回值。