Java - 获取当前类名?

363

我想做的就是获取当前类名,但Java会在我的类名末尾添加一个无用的非常规$1。如何摆脱它并仅返回实际的类名?

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

2
你是在哪里调用这个函数的?是在匿名内部类中吗?能否添加更多代码以显示类的定义细节和调用此行的位置? - plainjimbo
9
所以,你想要的只是 String className = getClass().getName().substring(0, getClass().getName().indexOf("$")) - josh.trow
12
如果你得到了 $1,那是因为这个类的名字是 $1。如果你期望得到其他内容,请在正确的类中使用 this 而不是错误的类。 - ceving
12个回答

-2

我发现这对我的代码有效,但是我的代码是在for循环中从数组中获取类。

String className="";

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

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

这并没有提出任何其他答案中尚未提及的内容。整个循环/数组问题是无关紧要的。此外,您应该了解一下代码格式化在问题/回答中的使用方式。 - Jonathon Reinhart

-4

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

2
你是否意识到你发布的答案是一个完整的代码块?这些语句不应该被格式化为代码引用。 - Massimiliano Kraus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接