从Abstract Class获取Concrete类名 [英] Get Concrete Class name from Abstract Class

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

问题描述

如果已经提出这个问题,我道歉。我搜索了这些问题并找不到答案。

I apologize if this question has been asked already. I searched the questions and could not find an answer.

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

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

如果有,我可以看一个代码示例吗?

If so, can I see a code example?

推荐答案

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

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.

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

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