1. Object类
Object
类是所有类的根父类,如果在类中没有extends
关键字指明其父类,默认其父类为Object类。
2. ==与equals方法的区别
==
:是否指向了同一个对象。
equals
:方法为Object
方法,只能比较引用类型,作用于==
相同,比较是否指向了同一个对象。但是,在某些判断中,只是比较其类的类型和内容,所以需要对该方法进行重写。
判断,只要根据两个对象的年月日相同,结果为true。重写equals方法。
public class MyDate {
//
private int day;
private int month;
private int year;
public MyDate(int day, int month, int year) {
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public boolean equals(Object o) {
// 比较 obj 和 this 是否是一个对象
if (this == o) // 完全是同一个对象 自然是相同的
return true;
if (o == null || getClass() != o.getClass()) // 待比较对象为空或者所属的类不同 不是
return false;
MyDate myDate = (MyDate) o; // 将object类强制转换为 MyDate类
if (day != myDate.day)
return false;
if (month != myDate.month)
return false;
return year == myDate.year;
}
@Override
public int hashCode() {
int result = day;
result = 31 * result + month;
result = 31 * result + year;
return result;
}
public static void main(String[] args) {
MyDate myDate = new MyDate(5,8,2021);
MyDate myDate1 = new MyDate(5,8,2021);
System.out.println(myDate.equals(myDate1)); // true
}
}
/*
true
*/
3. toString方法的重写
toString
方法在Object
类中定义,返回值是String
,返回类名和引用地址。
在
Person
类中重写toString
方法。
public class Person {
protected int age;
protected String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// 重写Object中的equals方法
public boolean equals(Object object){
if(object instanceof Person){
Person person = (Person) object;
return (person.getAge() == this.age)
&& (person.getName().equals(this.name));
}
return false;
}
@Override
public String toString() {
return "Person{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
public class TestToString {
public static void main(String[] args) {
Person person = new Person(12, "Tom");
System.out.println(person);
}
}
/*
Person{age=12, name='Tom'}
*/
4. 例题
重写
equals
和toString
方法,比较两个圆的半径是否相等;输出圆的半径。
public class GeometricObject {
protected String color;
protected double weight;
protected GeometricObject(){
color = "white";
weight = 1.0;
}
public GeometricObject(String color, double weight) {
this.color = color;
this.weight = weight;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
public class Circle extends GeometricObject{
private double radius;
public Circle(){
this.weight = 1.0;
this.color = "white";
this.radius = 1.0;
}
public Circle(double radius) {
this();
this.radius = radius;
}
public Circle(String color, double weight, double radius) {
super(color, weight);
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double findArea(){
return Math.PI * this.radius * this.radius;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Circle circle = (Circle) o;
return Double.compare(circle.radius, radius) == 0;
}
@Override
public int hashCode() {
long temp = Double.doubleToLongBits(radius);
return (int) (temp ^ (temp >>> 32));
}
@Override
public String toString() {
return "Circle{" +
"radius=" + radius +
'}';
}
}
public class TestCircle {
public static void main(String[] args) {
Circle circle1 = new Circle("red", 3,2);
Circle circle2 = new Circle("green", 3, 4);
System.out.println(circle1.getColor().equals(circle2.getColor()));
System.out.println(circle1.equals(circle2));
System.out.println(circle1);
}
}
/*
false
false
Circle{radius=2.0}
Circle{radius=4.0}
*/