62,621
社区成员
发帖
与我相关
我的任务
分享
private String getFunctionName()
{
StackTraceElement[] sts = Thread.currentThread().getStackTrace();
if (sts == null)
{
return null;
}
for (StackTraceElement st : sts)
{
if (st.isNativeMethod())
{
continue;
}
if (st.getClassName().equals(Thread.class.getName()))
{
continue;
}
if (st.getClassName().equals(this.getClass().getName()))
{
continue;
}
tag = st.getClassName();
return "[ "+ st.getFileName() + ":" + st.getLineNumber() + " "
+ st.getMethodName() + " ]";
}
return null;
}
public class Test {
public static void main(String[] args) {
//第一个,class就是代表这个类,比如本例中就代表B这个类而已。
Class<B> b= B.class;
//所以调用的是class类中的方法getName(),注意b是class这个类的类对象,而不是B的对象。
String name = b.getName();
}
}
class B{
}