如何在java中获取方法的字符串名称? [英] How to get string name of a method in java?

查看:440
本文介绍了如何在java中获取方法的字符串名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过反射找出该方法的字符串名称是什么?

How can I find out through reflection what is the string name of the method?

例如给定:

class Car{
   public void getFoo(){
   }
}

我想得到字符串getFoo,如下所示:

I want to get the string "getFoo", something like the following:

 Car.getFoo.toString() == "getFoo" // TRUE


推荐答案

由于方法本身不是对象,因此它们没有直接属性(就像您期望的JavaScript语言中的第一类函数一样)。

Since methods aren't objects themselves, they don't have direct properties (like you would expect with first-class functions in languages like JavaScript).

你最接近的是致电 Car.class.getMethods()

The closest you can do is call Car.class.getMethods()

Car.class Class 对象,您可以使用它来调用任何反射m方法。

Car.class is a Class object which you can use to invoke any of the reflection methods.

但据我所知,一种方法无法识别自身。

However, as far as I know, a method is not able to identify itself.

这篇关于如何在java中获取方法的字符串名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆