简体   繁体   中英

Get Concrete Class name from Abstract Class

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

Yes, you can do this by calling this.getClass() . This will give you the Class instance for the runtime type of this .

If you just want the name of the class, you could use this.getClass().getName() .

Lastly, there are also this.getClass().getSimpleName() and 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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