package Day02;
public class HomeWork {
public static void main(String[] args) {
Emp emp1 = new Emp("张三", 23, "男", 5000) ;
Emp emp2 = new Emp("张三", 23, "男", 5000) ;
System.out.println(emp1);
if (emp1.equals(emp2)) {
System.out.println("相等");
}else {
System.out.println("不相等");
}
}
}
/**
* 定义Emp类
* @author asus
*
*/
class Emp{
String name ;
int age ;
String sex ;
double money ;
Emp(String name , int age , String sex , double money ){
this.name = name ;
this.age = age ;
this.sex = sex ;
this.money = money ;
}
@Override
public String toString() {
return "name" + name + "age" + age + "sex" + sex + "money" + money;
}
/**
* 比较是不是同一个类型
* @param obj 是传入的对象
* @return true
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false ;
}
if (obj == this) {
return true ;
}
if (obj instanceof Emp) {
Emp e = (Emp) obj ;
return e.name .equals(this.name) &&
e.age == this.age &&
e.money == this.money &&
e.sex.equals(this.sex) ;
}
return false ;
}
public class HomeWork {
public static void main(String[] args) {
Emp emp1 = new Emp("张三", 23, "男", 5000) ;
Emp emp2 = new Emp("张三", 23, "男", 5000) ;
System.out.println(emp1);
if (emp1.equals(emp2)) {
System.out.println("相等");
}else {
System.out.println("不相等");
}
}
}
/**
* 定义Emp类
* @author asus
*
*/
class Emp{
String name ;
int age ;
String sex ;
double money ;
Emp(String name , int age , String sex , double money ){
this.name = name ;
this.age = age ;
this.sex = sex ;
this.money = money ;
}
@Override
public String toString() {
return "name" + name + "age" + age + "sex" + sex + "money" + money;
}
/**
* 比较是不是同一个类型
* @param obj 是传入的对象
* @return true
*/
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false ;
}
if (obj == this) {
return true ;
}
if (obj instanceof Emp) {
Emp e = (Emp) obj ;
return e.name .equals(this.name) &&
e.age == this.age &&
e.money == this.money &&
e.sex.equals(this.sex) ;
}
return false ;
}
}