java怎么获取当前方法名
lewis
2016-10-24
22次阅读
在Java中,可以通过反射的方式获取当前方法名。下面是一个示例代码:
public class Test {
public static void main(String[] args) {
String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
System.out.println("Current method name: " + methodName);
}
}
在这个示例中,通过创建一个匿名内部类的对象,并调用getClass().getEnclosingMethod().getName()方法来获取当前方法的名称。这种方法虽然有些繁琐,但可以实现获取当前方法名的功能。

发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。