我所要做的就是获取当前的类名,而java在我的类名的末尾附加了一个无用的无意义的$1。我怎么能摆脱它,只返回实际的类名?

String className = this.getClass().getName();

当前回答

试试这个 this.getClass(). getcanonicalname()或this.getClass(). getsimplename()。如果是匿名类,则使用this.getClass().getSuperclass().getName()

其他回答

试试这个 this.getClass(). getcanonicalname()或this.getClass(). getsimplename()。如果是匿名类,则使用this.getClass().getSuperclass().getName()

我假设这发生在一个匿名类中。当你创建一个匿名类时,你实际上创建了一个类,它扩展了你所获得的类名。

获得你想要的名字的“干净”方式是:

如果你的类是一个匿名的内部类,getSuperClass()应该给你一个创建它的类。如果你从一个接口创建它,那么你就是SOL因为你能做的最好的就是getInterfaces()它可能会给你多个接口。

“hack”的方法是使用getClassName()获取名称,并使用正则表达式删除$1。

这是一个Android版本,但同样的原则也可以用在纯Java中。

private static final String TAG = YourClass.class.getSimpleName();
private static final String TAG = YourClass.class.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.

你可以像这样使用this.getClass().getSimpleName():

import java.lang.reflect.Field;

public class Test {

    int x;
    int y;  

    public String getClassName() {

        String className = this.getClass().getSimpleName(); 
        System.out.println("Name:" + className);
        return className;
    }

    public Field[] getAttributes() {

        Field[] attributes = this.getClass().getDeclaredFields();   
        for(int i = 0; i < attributes.length; i++) {
            System.out.println("Declared Fields" + attributes[i]);    
        }

        return attributes;
    }

    public static void main(String args[]) {

        Test t = new Test();
        t.getClassName();
        t.getAttributes();
    }
}