我有一个application类,它是clientapplication的基类。应用程序有一个main方法,它打印为调用main方法而定义的类的名称。
public abstract class Application {
// Using this class as the entry point would output "Application"
public static void main ( String... args ) {
// Print the name of the class that is calling the main method
}
}
public class ClientApplication extends Application {
// Using this class as the entry point would output "ClientApplication"
// main is inherited and can be invoked via the ClientApplication class.
}
我尝试使用示例化对象来获取类的名称,如下所示:
public abstract class Application {
public static void main( String... args ) {
System.out.println( new Object() {}.getClass().getEnclosingClass().getSimpleName() );
}
}
但无论哪个类被定义为包含main方法的类,它都会打印出“application”。
1条答案
按热度按时间cbeh67ev1#
使用此选项,您将获得调用方类名
}