本文介绍了Class.getFields()返回的字段顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Javadoc> Class.getFields()说:返回的数组中的元素没有排序,也没有按任何特定的顺序排列。

Javadoc for Class.getFields() say: "The elements in the array returned are not sorted and are not in any particular order."

有关如何确定订单的任何提示?有可能当我执行两次这个方法时,我得到不同顺序的字段吗?换句话说,对于给定的编译类,或者甚至是相同源文件的编译之间的顺序是否稳定?

Any hints on how the order actually is determined? Is it possible that when I execute this method twice, I get fields in different order? In other words, is the order stable for given compiled class, or even between compilations of the same source file?

推荐答案

它应该要稳定,对于Oracle的JVM,它们的声明顺序,但你不应该依赖它。

It should be stable, and for Oracle's JVM its the order they are declared, but you should not rely on this.

你应该根据字段的名称(可能是声明类)来查找而不是位置。

You should base lookup on the field's name (and possibly declaring class) rather than position.

这篇关于Class.getFields()返回的字段顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 13:22