JAVA [ 类对象比较 ]

本文详细介绍了面向对象编程中的客户类实现,包括构造函数、属性获取与设置方法、equals()方法及hashCode()方法的使用,展示了如何通过类来封装和操作现实世界中的对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Customer{

private String name;
private int age;

public Customer(String name,int age){
this.name = name;
this.age = age;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

public boolean equals(Object o){
if(this == o) return true;
if(!(o instanceof Customer)) return false;
final Customer other = (Customer)o;
if(this.name.equals(other.getName())&& this.age == other.getAge())
return true;
else
return false;
}

public int hashCode(){
int result;
result = (name == null?0:name.hashCode());
result = 29 * result + age;
return result;

}


/*
*                       collection.Customer@03   collection.Customer@04 
* hashCode   |01|  |02|  |03|                   |04|                     ...  |09|
* Object            o3    o5    o2   
*                         o1    o4    o6(o7)[通过equals判别]
*/

//if a.equals(b), then a and b must have the same hash code.

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值