How to get string name of a method in java?

前端 未结 7 705
梦谈多话
梦谈多话 2020-11-29 06:10

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

For example given:

class Car{
   public void getFoo(){
   }
}

7条回答
  •  时光说笑
    2020-11-29 06:35

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

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

    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.

提交回复
热议问题