jafsldkfj 2013-10-03 03:46 采纳率: 0%
浏览 1846
已采纳

在JAVA反射中间遇到的超奇怪的问题!

public static void main(String[] args) {
UserBean userBean=new UserBean();
Method[] methods=userBean.getClass().getMethods();
String name=methods[2].getName();
System.out.println(name);
}

}
输出的是getUid(),没有错
Klaus。(741691740) 11:23:20
但是:
public static void main(String[] args) {
UserBean userBean=new UserBean();
Method[] methods=userBean.getClass().getMethods();
String name=methods[2].getName();
System.out.println(name);
System.out.println(name.equals("getUid"));
System.out.println(name);
}

}
我多加了一句话。怎么就变成了输出getVaules了?

  • 写回答

1条回答 默认 最新

  • solomon5926 2013-10-04 06:11
    关注

    你看帮助文档就能找到答案了,下面我贴一段Class类中getMethod()方法的描述,里面说得非常清楚,获得的Class数组是没有排序的。
    public Method[] getMethods() throws SecurityException
    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.

    看这句话
    The elements in the array returned are not sorted and are not in any particular order.
    当你修改代码之后就,Method[]中的元素的顺序就会被打乱。

    不知道我给的答案是不是完全正确,期待更好的解释。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog