Java反射:类字段和方法的顺序是否标准化? [英] Java reflection: Is the order of class fields and methods standardized?

查看:464
本文介绍了Java反射:类字段和方法的顺序是否标准化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java类上的反射来访问所有字段,方法等:

这些元素是否有标准化顺序(在某些标准中指定)?

Using reflection on Java classes to access all field, methods, and so on:
Is there a standardized order of these elements (which is specified in some standard)?

当然,我可以凭经验检查它,但我需要知道它是否总是
相同。

Of course, I could check it empirically, but I need to know if it's always the same.

编辑:

我等待问题:我需要订购的是什么;)

长话短说:我有JAXB注释类,并且不希望
直观地表示这些类。虽然XML属性
的顺序既不与XML
标准相关,也不适用于JAXB,但我希望对
可视化表示的XML属性有一定的顺序。

例如:start结束。这会伤害一个人的直觉。

推荐答案

根据文档

getFields()


返回一个包含Field对象的数组,这些对象反映了类或接口的所有可访问的公共字段由此Class对象表示。 返回的数组中的元素没有排序,也没有任何特定的顺序。如果类或接口没有可访问的公共字段,或者它表示数组类,则此方法返回长度为0的数组,原始类型或空白。

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if the class or interface has no accessible public fields, or if it represents an array class, a primitive type, or void.

getMethods()


返回一个数组,其中包含反映此Class对象所表示的类或接口的所有公共成员方法的Method对象,包括由类或接口声明的那些方法以及从超类和超接口继承的方法。数组类返回从Object类继承的所有(公共)成员方法。 返回的数组中的元素没有排序,并且没有按任何特定的顺序排列。如果此Class对象表示没有公共成员方法的类或接口,或者如果此类方法表示没有公共成员方法的类或接口,则此方法返回长度为0的数组。此Class对象表示基本类型或void。

Returns an array containing Method objects reflecting all the public member methods of the class or interface represented by this Class object, including those declared by the class or interface and those inherited from superclasses and superinterfaces. Array classes return all the (public) member methods inherited from the Object class. The elements in the array returned are not sorted and are not in any particular order. This method returns an array of length 0 if this Class object represents a class or interface that has no public member methods, or if this Class object represents a primitive type or void.

这篇关于Java反射:类字段和方法的顺序是否标准化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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