我所要做的就是获取当前的类名,而java在我的类名的末尾附加了一个无用的无意义的$1。我怎么能摆脱它,只返回实际的类名?
String className = this.getClass().getName();
我所要做的就是获取当前的类名,而java在我的类名的末尾附加了一个无用的无意义的$1。我怎么能摆脱它,只返回实际的类名?
String className = this.getClass().getName();
当前回答
反射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.
其他回答
Try,
String className = this.getClass().getSimpleName();
只要你不在静态方法中使用它,它就可以工作。
我假设这发生在一个匿名类中。当你创建一个匿名类时,你实际上创建了一个类,它扩展了你所获得的类名。
获得你想要的名字的“干净”方式是:
如果你的类是一个匿名的内部类,getSuperClass()应该给你一个创建它的类。如果你从一个接口创建它,那么你就是SOL因为你能做的最好的就是getInterfaces()它可能会给你多个接口。
“hack”的方法是使用getClassName()获取名称,并使用正则表达式删除$1。
在您的示例中,这可能引用一个匿名类实例。Java通过在外围类的名称后面附加一个$number来为这些类命名。
两个答案的组合。还输出一个方法名:
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.