简体   繁体   English

从抽象类中获取具体类名

[英]Get Concrete Class name from Abstract Class

在 Java 中,我可以在抽象类中获取扩展它的具体类的实例吗?

Yes, you can do this by calling this.getClass() .是的,您可以通过调用this.getClass()来做到这一点。 This will give you the Class instance for the runtime type of this .这将为您提供this的运行时类型的Class实例。

If you just want the name of the class, you could use this.getClass().getName() .如果您只想要类的名称,则可以使用this.getClass().getName()

Lastly, there are also this.getClass().getSimpleName() and this.getClass().getCanonicalName() .最后,也有this.getClass().getSimpleName()this.getClass().getCanonicalName() I use the former all the time to print readable class names to log files and the like.我一直使用前者将可读的类名打印到日志文件等。

Thread.currentThread().getStackTrace()[2].getClassName()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM