使用 getClass() relection 方法输出 class 的名称
using getClass() relection method to output name of class
我正在尝试让 class 使用 getClass() 打印它自己的名称。我也希望它包含包名。该包称为 "reflections",class 称为 ReflectionClass。这就是我目前所拥有的!
package reflections;
public class ReflectionClass {
public void className(){
String name = reflections.ReflectionClass.getClass().getName();
System.out.print(name);
}
}
试试这个
String className = this.getClass().getName();
System.out.println(className);
你可以使用
或
方法一
System.out.print((new ReflectionClass()).getClass().getName());
因为你可以用静态方式引用非静态方法getClass()
。
方法二
It is also possible to get the Class object for a named type (or for
void) using a class literal (JLS Section 15.8.2). For example:
System.out.println(ReflectionClass.class.getName());
我正在尝试让 class 使用 getClass() 打印它自己的名称。我也希望它包含包名。该包称为 "reflections",class 称为 ReflectionClass。这就是我目前所拥有的!
package reflections;
public class ReflectionClass {
public void className(){
String name = reflections.ReflectionClass.getClass().getName();
System.out.print(name);
}
}
试试这个
String className = this.getClass().getName();
System.out.println(className);
你可以使用
或
方法一
System.out.print((new ReflectionClass()).getClass().getName());
因为你可以用静态方式引用非静态方法getClass()
。
方法二
It is also possible to get the Class object for a named type (or for void) using a class literal (JLS Section 15.8.2). For example:
System.out.println(ReflectionClass.class.getName());