Reflection api - 查找子类类型

发布于 11-13 06:06 字数 421 浏览 2 评论 0原文

嗨,我正在尝试找出子类,形成一个超类对象。

if Class Super is the super class.
and Class Sub1 and Class Sub2 extends Class Super.(all classes are public)

Lets say I have a object of Super type as
Super superObject = new Sub1();

现在对于 superObject,是否可以找到 superObject 在 Java 中扩展了哪个子类?

由于“超类不会知道它拥有的任何子类”,您能否告诉我上面的问题首先是有效,还是我遗漏了任何基本概念? >

提前致谢。

Hi I am trying to find out the subclass, form an superclass object.

if Class Super is the super class.
and Class Sub1 and Class Sub2 extends Class Super.(all classes are public)

Lets say I have a object of Super type as
Super superObject = new Sub1();

now for the superObject, is it possible to find which subclass the superObject extends in Java?

Since "SuperClass will not be aware of any SubClasses it has", can you please tell me is my above question valid in the first place, or am I missing any basic concept?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荒芜了季节2024-11-20 06:06:05

getClass() 方法将在运行时返回对象的具体类型,而不是引用类型。所以简单地做:

Super superObject = new Sub1();
Class<? extends Super> klass = superObject.getClass();

会给你 Sub1.class 从你的问题中不清楚你想用它做什么。您可以简单地调用 getName() ,检查您拥有的其他引用是否具有相同类型,等等。

The getClass() method will return the concrete type of the object at runtime, not the reference type. So simply doing:

Super superObject = new Sub1();
Class<? extends Super> klass = superObject.getClass();

Will give you Sub1.class It's not clear from your question what you then want to do with it. You can simply call getName() on it, check if some other reference you have is of the same type, etc etc.

痴情2024-11-20 06:06:05

你的问题,“是否有可能找到 superObject 在 Java 中扩展的子类?”,没有任何意义。

无论如何,我假设您正在寻找 Class.isAssignableFrom() 可能在这方面有所帮助。

用法:

superClass.isAssignableFrom(subClass);

文档:

判断类还是接口
这个类对象表示的是
要么相同,要么是超类
或类的超级接口或
由指定的接口表示
类参数。

参考

Your question, "is it possible to find which subclass the superObject extends in Java?", is not making any sense.

Anyway, I assume you are looking for Class.isAssignableFrom() might help in this regards.

Usage:

superClass.isAssignableFrom(subClass);

Docs:

Determines if the class or interface
represented by this Class object is
either the same as, or is a superclass
or superinterface of, the class or
interface represented by the specified
Class parameter.

Reference

画▽骨i2024-11-20 06:06:05

您需要此方法:

boolean isList = Subclass.isAssignableFrom(Superclass);

来自 JavaDoc:

确定此 Class 对象表示的类或接口是否与指定 Class 表示的类或接口相同,或者是其超类或超接口范围。如果是则返回 true;否则返回 false。如果此 Class 对象表示基本类型,且指定的 Class 参数正是此 Class 对象,则此方法返回 true;否则返回 false。

参考:

 Class.isAssignableFrom(Class)

You want this method:

boolean isList = Subclass.isAssignableFrom(Superclass);

From the JavaDoc:

Determines if the class or interface represented by this Class object is either the same as, or is a superclass or superinterface of, the class or interface represented by the specified Class parameter. It returns true if so; otherwise it returns false. If this Class object represents a primitive type, this method returns true if the specified Class parameter is exactly this Class object; otherwise it returns false.

Reference:

 Class.isAssignableFrom(Class)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文