读书人

关于equals()跟hashcode()方法的一些实

发布时间: 2012-08-27 21:21:56 作者: rapoo

关于equals()和hashcode()方法的一些实验

1、新建一Student.java,包含3个field,和相应的get、set方法。

 /**     * Compares this string to the specified object.  The result is {@code     * true} if and only if the argument is not {@code null} and is a {@code     * String} object that represents the same sequence of characters as this     * object.     *     * @param  anObject     *         The object to compare this {@code String} against     *     * @return  {@code true} if the given object represents a {@code String}     *          equivalent to this string, {@code false} otherwise     *     * @see  #compareTo(String)     * @see  #equalsIgnoreCase(String)     */    public boolean equals(Object anObject) {if (this == anObject) {    return true;}if (anObject instanceof String) {    String anotherString = (String)anObject;    int n = count;    if (n == anotherString.count) {char v1[] = value;char v2[] = anotherString.value;int i = offset;int j = anotherString.offset;while (n-- != 0) {    if (v1[i++] != v2[j++])return false;}return true;    }}return false;    }

?

?

?http://huangqiqing123.iteye.com/blog/1462404

?

?

读书人网 >编程

热点推荐