Object测试
手机类
package ObjectExercise20240716;
import java.util.Objects;
public class Phone {
String brand;
int price;
public Phone() {}
public Phone(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Phone{brand:" + this.brand + " price:" + this.price
+ "}" ;
}
@Override
public boolean equals(Object o) // 相当于这里是用Object顶级父类来接受传递的对象,从而形成多态
{
if (this == o) return true; // 如果两个对象是同一个对象则直接返回ture(同一个对象必然相等)
if (o == null || getClass() != o.getClass()) return false;
// 当传递的对象是null,或者两个对象不是同类,则返回false,无需多言
Phone phone = (Phone) o; // 强制类型转换,从父类到子类
return price == phone.price && Objects.equals(brand, phone.brand);
// 现在比较的就是属性值了
}
}
测试
package ObjectExercise20240716;
import java.util.Scanner;
public class ObjectExercise {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Object obj = new Object();
Phone phone = new Phone("iqoo", 2299);
System.out.println(phone.toString()); //返回该对象的字符串表示形式(可以看做是对象的内存地址值)
// Object 中的 toString是返回的内存地址值的字符串表示形式,大部分情况下无意义,需要在子类中重写
Phone phone2 = new Phone("iqoo", 2299);
//System.out.println(phone2.equals(phone)); // Object中的equals方法比较的仍然是两个对象的地址值,需要重写
// 源码中Object比较用的是“ == ”,比较的是地址值
System.out.println(phone2.equals(phone));
}
}