package top.qaqaq.java.P301;
import java.util.Objects;
public class Customer {
private String name;
private int age;
public Customer() {
super();
}
public Customer(String name, int age) {
super();
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;
}
// 自动生成的 equals()
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Customer other = (Customer) obj;
return age == other.age && Objects.equals(name, other.name);
}
// 重写的原则:比较两个对象的实体内容(即:name和age)是否相同
// 手动实现 equals() 的重写
// @Override
// public boolean equals(Object obj) {
//
//// System.out.println("Customer equals()...");
// if (this == obj) {
// return true;
// }
//
// if(obj instanceof Customer) {
// Customer cust = (Customer)obj;
// //比较两个对象的每个属性是否都相同
//// if(this.age == cust.age && this.name.equals(cust.name)) {
//// return true;
//// }else {
//// return false;
//// }
//
// //或
// return this.age == cust.age && this.name.equals(cust.name);
// }else {
// return false;
// }
//
// }
// 手动实现
// @Override
// public String toString() {
// return "Customer[name = " + name + ",age = " + age + "]";
// }
// 自动实现
@Override
public String toString() {
return "Customer [name=" + name + ", age=" + age + "]";
}
}
package top.qaqaq.java.P301;
import java.util.Date;
/*
* Object类中toString()的使用:
*
* 1. 当我们输出一个对象的引用时,实际上就是调用当前对象的toString()
*
* 2. Object类中toString()的定义:
* public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
*
* 3. 像String、Date、File、包装类等都重写了Object类中的toString()方法。
* 使得在调用对象的toString()时,返回"实体内容"信息
*
* 4. 自定义类也可以重写toString()方法,当调用此方法时,返回对象的"实体内容"
*
*/
public class ToStringTest {
public static void main(String[] args) {
Customer cust1 = new Customer("Tom", 21);
System.out.println(cust1.toString());//top.qaqaq.java.P301.Customer@5aaa6d82-->Customer[name = Tom,age = 21]
System.out.println(cust1);//top.qaqaq.java.P301.Customer@5aaa6d82-->Customer[name = Tom,age = 21]
String str = new String("MM");
System.out.println(str);//MM
Date date = new Date(4534534534543L);
System.out.println(date.toString());//Mon Sep 11 08:55:34 CST 2113
}
}