equals method should have a hashCode method - noticed by Eric Nickell
$self special variable for %extend git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@9529 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
be61c5347b
commit
0cccd3cabe
1 changed files with 8 additions and 3 deletions
|
|
@ -3449,7 +3449,7 @@ struct Vector {
|
|||
%extend Vector {
|
||||
char *toString() {
|
||||
static char tmp[1024];
|
||||
sprintf(tmp,"Vector(%g,%g,%g)", self->x,self->y,self->z);
|
||||
sprintf(tmp,"Vector(%g,%g,%g)", $self->x,$self->y,$self->z);
|
||||
return tmp;
|
||||
}
|
||||
Vector(double x, double y, double z) {
|
||||
|
|
@ -6239,7 +6239,9 @@ Note that the JNI code above uses a number of string lookups to call a construct
|
|||
When a pointer is returned from a JNI function, it is wrapped using a new Java proxy class or type wrapper class.
|
||||
Even when the pointers are the same, it will not be possible to know that the two Java classes containing those pointers are actually the same object.
|
||||
It is common in Java to use the <tt>equals()</tt> method to check whether two objects are equivalent.
|
||||
An equals method is easily added to all proxy classes. For example:
|
||||
The <tt>equals()</tt> method is usually accompanied by a <tt>hashCode()</tt> method in order to fulfill
|
||||
the requirement that the hash code is equal for equal objects.
|
||||
Pure Java code methods like these can be easily added:
|
||||
</p>
|
||||
|
||||
<div class="code">
|
||||
|
|
@ -6251,6 +6253,9 @@ An equals method is easily added to all proxy classes. For example:
|
|||
equal = ((($javaclassname)obj).swigCPtr == this.swigCPtr);
|
||||
return equal;
|
||||
}
|
||||
public int hashCode() {
|
||||
return (int)getPointer();
|
||||
}
|
||||
%}
|
||||
|
||||
class Foo { };
|
||||
|
|
@ -6497,7 +6502,7 @@ To implement this, we use the above interface file code but remove the <tt>javac
|
|||
return pButler;
|
||||
}
|
||||
~Butler() {
|
||||
FireButler(self);
|
||||
FireButler($self);
|
||||
}
|
||||
}
|
||||
</pre>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue