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

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

当前回答

在您的示例中,这可能引用一个匿名类实例。Java通过在外围类的名称后面附加一个$number来为这些类命名。

其他回答

反射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.

我发现这适用于我的代码,但是我的代码是在for循环中从数组中获取类。

String className="";

className = list[i].getClass().getCanonicalName();

System.out.print(className); //Use this to test it works

你可以像这样使用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();
    }
}

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

private static final String TAG = YourClass.class.getSimpleName();
private static final String TAG = YourClass.class.getName();

Try,

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

只要你不在静态方法中使用它,它就可以工作。