美文网首页
Java获取当前类名的两种方法

Java获取当前类名的两种方法

作者: 凌天_0e52 | 来源:发表于2018-11-06 14:13 被阅读0次

获取类名:

1、在类的实例中可使用this.getClass().getName();但在static method中不能使用该方法;

2、在static method中使用方法:Thread.currentThread().getStackTrace()[1].getClassName();

获取方法名:Thread.currentThread().getStackTrace()[1].getMethodName();

获取代码行号:Thread.currentThread().getStackTrace()[1].getLineNumber();

Log 代码:

System.out.println("Class: "+this.getClass().getName()+" method: "+

Thread.currentThread().getStackTrace()[1].getMethodName() +" line:"+

Thread.currentThread().getStackTrace()[1].getLineNumber());

相关文章

网友评论

      本文标题:Java获取当前类名的两种方法

      本文链接:https://www.haomeiwen.com/subject/nituxqtx.html