
Java面向对象-Object类的toString方法、equals方法
- 一、toString
 - 二、equals
 - 三、总结
 
一、toString
Object的toString方法。
 方法的原理:
 
 现在使用toString方法的时候,打印出来的内容不友好。
 现在想要知道对象的信息。
 
 出现的问题:子类Student对父类提供的toString方法不满意

 
 总结:toString的作用就是对对象进行“自我介绍”,一般子类对父类提供的toString都不满意,都需要重写。

 idea自带快捷键自动生成
二、equals
这个方法提供了对对象的内容是否相等的一个比较方式。对象的内容就是属性。父类提供的equals就是比较==地址,没有实际意义。我们一般不会直接使用父类提供的方法,而是在子类中对这个方法进行重写。
package com.msb_equal;
public class Phone {
    private String brand;
    private double price;
    private int year;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    @Override
    public String toString() {
        return "Phone{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", year=" + year +
                '}';
    }
    //todo 对equals方法进行重写
    public boolean equals(Object obj) {
        return (this == obj);
    }
    public Phone(){
    }
    public Phone(String brand, double price, int year) {
        this.brand = brand;
        this.price = price;
        this.year = year;
    }
}
 
==:比较左右两侧的值是否相等
 对于引用数据类型来说,比较的是地址。返回的是false
 equals(p2); //点击源码发现:底层依旧比较的是==
package com.msb_equal;
public class Test {
    public static void main(String[] args) {
        Phone p1 = new Phone("xiaomi14",4300.00,2023);
        Phone p2 = new Phone("xiaomi14",4300.00,2023);
        //比较2个对象,p1和p2
        //==:比较左右两侧的值是否相等
        System.out.println(p1==p2);   //对于引用数据类型来说,比较的是地址。返回的是false
        //Object类提供了一个方法equals,作用比较对象具体内容是否相等。
        boolean flag= p1.equals(p2); //点击源码发现:底层依旧比较的是==
        System.out.println(flag);   //false
    }
}
 
idea自动生成equals方法对父类进行重写
 

三、总结
类和类可以产生关系:
- 将一个类作为另一个类中的方法的形参
 - 将一个类作为另一个类中的属性
 
package com.msb_class;
public class Boy {
    //属性
    int age;
    String name;
    //方法
    public void buy() {
        System.out.println("买");
    }
    //构造器
    public Boy() {
    }
    public Boy(int age, String name) {
        this.age = age;
        this.name = name;
    }
}
 
package com.msb_class;
public class Girl {
    //属性
    String name;
    double weight;
    Mon m = new Mon();
    //方法
    public void add(int a) {  //todo 基本数据类型
        System.out.println(a);
    }
    public void love(Boy b) {  // todo 参数是引用数据类型
        System.out.println("name为:" + b.name + ",age为:" + b.age);
        b.buy();
    }
    public void wechat() {
        m.say();
    }
    //构造器
    public Girl() {
    }
    public Girl(String name, double weight) {
        this.name = name;
        this.weight = weight;
    }
}
 
package com.msb_class;
public class Mon {
    public void say(){
        System.out.println("say");
    }
}
 
package com.msb_class;
public class Test {
    public static void main(String[] args) {
        Boy boy = new Boy(30, "kobe");
        Girl girl = new Girl("wns", 100);
        girl.love(boy);
		
		//方法1
        girl.wechat();
		
		//方法2
        girl.m = new Mon();
        girl.wechat();
    }
}
                


















