Java笔记(7)——equals和toString方法的重写和一个例子

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. 例题

重写equalstoString方法,比较两个圆的半径是否相等;输出圆的半径。

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}
*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值