我所要做的就是获取当前的类名,而java在我的类名的末尾附加了一个无用的无意义的$1。我怎么能摆脱它,只返回实际的类名?
String className = this.getClass().getName();
我所要做的就是获取当前的类名,而java在我的类名的末尾附加了一个无用的无意义的$1。我怎么能摆脱它,只返回实际的类名?
String className = this.getClass().getName();
当前回答
两个答案的组合。还输出一个方法名:
Class thisClass = new Object(){}.getClass();
String className = thisClass.getEnclosingClass().getSimpleName();
String methodName = thisClass.getEnclosingMethod().getName();
Log.d("app", className + ":" + methodName);
其他回答
两个答案的组合。还输出一个方法名:
Class thisClass = new Object(){}.getClass();
String className = thisClass.getEnclosingClass().getSimpleName();
String methodName = thisClass.getEnclosingMethod().getName();
Log.d("app", className + ":" + methodName);
反射api
There are several Reflection APIs which return classes but these may only be accessed if a Class has already been obtained either directly or indirectly. Class.getSuperclass() Returns the super class for the given class. Class c = javax.swing.JButton.class.getSuperclass(); The super class of javax.swing.JButton is javax.swing.AbstractButton. Class.getClasses() Returns all the public classes, interfaces, and enums that are members of the class including inherited members. Class<?>[] c = Character.class.getClasses(); Character contains two member classes Character.Subset and Character.UnicodeBlock. Class.getDeclaredClasses() Returns all of the classes interfaces, and enums that are explicitly declared in this class. Class<?>[] c = Character.class.getDeclaredClasses(); Character contains two public member classes Character.Subset and Character.UnicodeBlock and one private class Character.CharacterCache. Class.getDeclaringClass() java.lang.reflect.Field.getDeclaringClass() java.lang.reflect.Method.getDeclaringClass() java.lang.reflect.Constructor.getDeclaringClass() Returns the Class in which these members were declared. Anonymous Class Declarations will not have a declaring class but will have an enclosing class. import java.lang.reflect.Field; Field f = System.class.getField("out"); Class c = f.getDeclaringClass(); The field out is declared in System. public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); } The declaring class of the anonymous class defined by o is null. Class.getEnclosingClass() Returns the immediately enclosing class of the class. Class c = Thread.State.class().getEnclosingClass(); The enclosing class of the enum Thread.State is Thread. public class MyClass { static Object o = new Object() { public void m() {} }; static Class<c> = o.getClass().getEnclosingClass(); } The anonymous class defined by o is enclosed by MyClass.
“1美元”不是“无用的废话”。如果您的类是匿名的,则会追加一个数字。
如果你不想要类本身,而是声明类,那么你可以使用getEnclosingClass()。例如:
Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
System.out.println(enclosingClass.getName());
} else {
System.out.println(getClass().getName());
}
你可以把它移动到一些静态工具方法中。
但请注意,这不是当前的类名。匿名类与它的外围类不同。内部类的情况也类似。
在我的例子中,我使用这个Java类:
private String getCurrentProcessName() {
String processName = "";
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == pid) {
processName = processInfo.processName;
break;
}
}
return processName;
}
在您的示例中,这可能引用一个匿名类实例。Java通过在外围类的名称后面附加一个$number来为这些类命名。