这是一个奇怪的行为发生getDeclaredMethods
,这是场景,一个名为 Entity 的类:
public class Entity {
private Object reference;
/**
* @return the reference
*/
public Object getReference() {
return reference;
}
/**
* @param reference the reference to set
*/
public void setReference(Object reference) {
this.reference = reference;
}
public Object getReference2() {
return reference;
}
public void setReference2(Object reference) {
this.reference = reference;
}
}
和主类:
public static void main(String Args[]){
try {
Entity _entity = new Entity();
Class _newClass = _entity.getClass();
Method[] _method = _newClass.getDeclaredMethods();
for (int i = 0; i < _method.length; i++) {
System.out.println(_method[i]);
}
} catch (IllegalArgumentException | SecurityException ex) {
Logger.getLogger(MainApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
输出:
public java.lang.Object Entities.Entity.getReference()
public void Entities.Entity.setReference(java.lang.Object)
public java.lang.Object Entities.Entity.getReference2()
public void Entities.Entity.setReference2(java.lang.Object)
好吧,它们是按顺序排列的,没关系,但是当您获得对某些内容的引用时,就会发生这种情况_entity.setReference(100);
, 输出:
public void Entities.Entity.setReference(java.lang.Object)
public java.lang.Object Entities.Entity.getReference()
public java.lang.Object Entities.Entity.getReference2()
public void Entities.Entity.setReference2(java.lang.Object)
那么...为什么setReference
排在第一位?也许是因为它有价值?无论我设置什么字段,如何保持类文件中声明的顺序?