程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-在JAVA反射中間遇到的超奇怪的問題!

java-在JAVA反射中間遇到的超奇怪的問題!

編輯:編程綜合問答
在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了?

最佳回答:


你看幫助文檔就能找到答案了,下面我貼一段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[]中的元素的順序就會被打亂。

不知道我給的答案是不是完全正確,期待更好的解釋。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved