从抽象类中获取具体类名 [英] Get Concrete Class name from Abstract Class

查看:25
本文介绍了从抽象类中获取具体类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Java, inside an abstract class can I get the instance of the concrete class that extends it?

推荐答案

是的,您可以通过调用 this.getClass().这将为您提供 Class this 的运行时类型的实例.

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

如果你只想要类的名称,你可以使用 this.getClass().getName().

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

最后,还有<代码>this.getClass().getSimpleName()this.getClass().getCanonicalName().我一直使用前者将可读的类名打印到日志文件等.

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.

这篇关于从抽象类中获取具体类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆