Java - get the current class name?
我所要做的就是获取当前的类名,Java在我的类名末尾附加了一个无用的无意义的1美元。我怎样才能去掉它,只返回实际的类名呢?
1 |
"$1"不是"无用的无意义的"。如果您的类是匿名的,则会附加一个数字。
如果您不希望类本身,而是它的声明类,那么您可以使用
1 2 3 4 5 6 |
您可以在一些静态实用程序方法中移动它。
但请注意,这不是当前的类名。匿名类与其封闭类不同。内部类的情况类似。
尝试,
1 |
只要您不在静态方法中使用它,这就可以工作。
尝试使用这个
您可以使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | import java.lang.reflect.Field; public class Test { int x; int y; public void getClassName() { String className = this.getClass().getSimpleName(); System.out.println("Name:" + className); } public void getAttributes() { Field[] attributes = this.getClass().getDeclaredFields(); for(int i = 0; i < attributes.length; i++) { System.out.println("Declared Fields" + attributes[i]); } } public static void main(String args[]) { Test t = new Test(); t.getClassName(); t.getAttributes(); } } |
两个答案的组合。还打印方法名称:
1 2 3 4 |
这个答案很晚了,但我认为在匿名处理程序类的上下文中还有另一种方法可以做到这一点。
让我们说:
1 2 3 4 5 6 7 8 9 10 | class A { void foo() { obj.addHandler(new Handler() { void bar() { String className=A.this.getClass().getName(); // ... } }); } } |
它也会达到同样的效果。另外,它实际上非常方便,因为每个类都是在编译时定义的,所以不会破坏动态性。
除此之外,如果类确实是嵌套的,即
1 | B.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.
1
2
3
4
5
6
7 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.
1Character contains two member classes Character.Subset and
Character.UnicodeBlock.
1
2
3
4
5 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 classCharacter.CharacterCache.
1
2
3
4
5 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 willhave an enclosing class.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 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循环中的数组中获取类。
1 2 3 4 5 |