我自己實現(xiàn)了一個類class Method{String className,methodName;Vector parameterTypes;}并把Method作為鍵值,寫了一個Map然后我在遍歷輸出這個Map的所有Key時候,能夠輸出我想要的那個對象a的內容,可是我寫Map.containsKey(a)的時候,它卻返回了false。我嘗試過自己改寫Object.equals方法,但還是沒用。改寫的equals方法如下:public boolean equals(Object anotherMethod){
if(!(anotherMethod instanceof Method))
return false;
Method another = (Method)anotherMethod;
if(! this.className.equals(another.className))
return false;
if(! this.methodName.equals(another.methodName))
return false;
if(this.parameterTypes == null && another.parameterTypes != null)
return false;
if(this.parameterTypes != null && another.parameterTypes == null)
return false;
if(this.parameterTypes.size() != another.parameterTypes.size())
return false;
for(int i=0; i<this.parameterTypes.size(); i++){
if(!this.parameterTypes.elementAt(i).equals(another.parameterTypes.elementAt(i)))
return false;
}
return true;
}另外,我嘗試了如下代碼:methodMap.put(method, methodList.size());//methodList.size()是一個int
System.out.println(methodMap.containsKey(method));//輸出true
System.out.println(methodMap.containsKey(new Method(method)));//輸出false
System.out.println(method.equals(new Method(method)));//輸出true請問這是什么原因,應該如何解決?
添加回答
舉報
0/150
提交
取消