如何获取类的类型进行比较
我有这个对象,它是超类的实例。我想知道该对象到底是哪个子类,以便我可以决定如何处理它。有这个 getClass() 方法,但它显然不用于比较问题。如何获取对象的子类型?
I have this object which is an instance of a superclass. I want to know which subclass that object really is, so that I can decide what to do with it. There is this getClass() method but it's apparently not used for comparison issues. How can I get the sub-type of my object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试执行此操作,但
instanceof
,则可能存在设计缺陷。请参阅谨防instanceof 运算符。
You may have a design flaw if you're trying to do this but
instanceof
.See Beware of instanceof operator.
Class c = (你的超类名称).getClass();
if(c.getName == "你的子类名")
采取行动
Class c = (your super class name).getClass();
if(c.getName == "your sub class name")
take action