यहाँ जावा 7 के रूप में java.lang.reflect.Method.equals(Object obj)
के कार्यान्वयन है: getName() == other.getName()
:नाम तुलना (वस्तु obj)
/**
* Compares this {@code Method} against the specified object. Returns
* true if the objects are the same. Two {@code Methods} are the same if
* they were declared by the same class and have the same name
* and formal parameter types and return type.
*/
public boolean equals(Object obj) {
if (obj != null && obj instanceof Method) {
Method other = (Method)obj;
if ((getDeclaringClass() == other.getDeclaringClass())
&& (getName() == other.getName())) {
if (!returnType.equals(other.getReturnType()))
return false;
/* Avoid unnecessary cloning */
Class<?>[] params1 = parameterTypes;
Class<?>[] params2 = other.parameterTypes;
if (params1.length == params2.length) {
for (int i = 0; i < params1.length; i++) {
if (params1[i] != params2[i])
return false;
}
return true;
}
}
}
return false;
}
यहाँ सबसे दिलचस्प हिस्सा विधि के नाम की तुलना है। वे java.lang.String
लौटते हैं और इसलिए एक प्राकृतिक सवाल यह है कि क्या संदर्भों से तुलना करना वैध है (==
)। हालांकि यह कोड स्पष्ट रूप से सवाल करता है कि यह प्रतिबिंब उन्मुख ढांचे में बग का स्रोत हो सकता है। तुम क्या सोचते हो?
हो सकता है कि एक है जो लिखा है 'getName() == other.getName()' जांचना चाहती है कि क्या दो 'या नहीं name' संदर्भ स्मृति में एक ही वस्तु का उल्लेख है, बजाय वास्तविक स्ट्रिंग मानों की जांच करना। –