資源簡介
Java 重新計算對象的哈希碼(基礎篇-實例105).zip
代碼片段和文件信息
package?com.mingrisoft.cat;
import?java.awt.Color;
public?class?Cat?{
????private?String?name;
????private?int?age;
????private?double?weight;
????private?Color?color;
????
????public?Cat(String?name?int?age?double?weight?Color?color)?{
????????this.name?=?name;
????????this.age?=?age;
????????this.weight?=?weight;
????????this.color?=?color;
????}
????
????@Override
????public?boolean?equals(object?obj)?{
????????if?(this?==?obj)?{
????????????return?true;
????????}
????????if?(obj?==?null)?{
????????????return?false;
????????}
????????if?(getClass()?!=?obj.getClass())?{
????????????return?false;
????????}
????????Cat?cat?=?(Cat)?obj;
????????return?name.equals(cat.name)?&&?(age?==?cat.age)?&&?(weight?==?cat.weight)?&&?(color.equals(cat.color));
????}
????
????@Override
????public?int?hashCode()?{
????????return?7?*?name.hashCode()?+?11?*?new?Integer(age).hashCode()?+?13?*?new?Double(weight).hashCode()?+?17?*?color.hashCode();
????}
}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件?????????232??2010-09-11?17:02??.classpath
?????文件?????????379??2010-09-11?17:02??.project
?????文件????????1169??2013-11-21?08:41??bin\com\mingrisoft\cat\Cat.class
?????文件????????1423??2013-11-21?08:41??bin\com\mingrisoft\cat\Test.class
?????文件?????????992??2010-09-11?17:02??src\com\mingrisoft\cat\Cat.java
?????文件?????????669??2010-09-11?17:02??src\com\mingrisoft\cat\Test.java
- 上一篇:Java 解決線程的死鎖問題
- 下一篇:Java 算數異常
評論
共有 條評論