Java中重写toString和equals

2021.02.13
第八次记录
课堂笔记1:
/*
关于object类中的toString方法:
1.源代码:
public String toString(){
return getClass().getName() + ‘@’ + Integer.toHexString(hashCode());
}
源代码上toString()方法的默认实现是:
类名@对象的内存地址转换为十六进制的形式
2.SUN公司设计的toString()方法的目的:
通过调用这个方法可以将一个"java对象"转换成"字符串"的表现形式
3.SUN公司开发java语言时,建议所有子类都去重写toString()方法。
结果应是一个简明但易于读懂的信息表达式
*/
代码演示1:

public class reWritetoString {
    
    
    public static void main(String[] args) {
    
    
        Myworld myworld = new Myworld(1990, 11, 29);
        String s = myworld.toString();
        System.out.println(s);
    }
}
class Myworld {
    
    
    //定义属性
    int year;
    int month;
    int day;
    //无参构造
    public Myworld(){
    
    

    }
    //有参构造
    public Myworld(int year, int month, int day){
    
    
        this.year = year;
        this.month = month;
        this.day = day;
    }
    //重写子类toString方法
    public String toString(){
    
    
        return this.year + "/" + this.month + "/" + this.day;
    }
}

输出结果:
1990/11/29
课堂笔记2
/*
关于object类中的equals方法
1.equals方法的源代码:

public boolean equals(Object obj) {
    
    
        return (this == obj);
    }
以上这个方法是object类的默认实现

2.SUN公司设计equals方法的目的是什么?
以后的编程过程当中,都要通过equals方法来判断两个对象是否相等。
equals方法是判断两个对象是否相等的。
3.我们需要研究一下object类给的这个默认的equals方法够不够用?
在object类中的equals方法当中,默认采用的是"“来判断两个对象是否相等。
而”
“判断的是两个java对象的内存地址,我们应该判断两个java对象的内容是否相等。
所有object类中的equals方法不够用,需要重写子类。
4.判断两个java对象是否相等,不能使用”",因为""比较的是两个对象的内存地址
*/
代码演示2:

 public class Aboutequals {
    
    
    public static void main(String[] args) {
    
    
        //判断两个基本数据类型是否相等直接使用"=="就行
        int a = 100;
        int b = 100;
        System.out.println(a == b); //true
        //判断两个java对象是否相等,能直接使用"=="吗?
        MyTime02 t1 = new MyTime02(2021, 02, 13);
        String s = t1.toString();
        System.out.println(s);
        MyTime02 t2 = new MyTime02(2021, 02, 13);
        //这里的"=="判断的是:MyTime中保存的对象内存地址和MyTime2中保存的对象内存地址是否相等
        //重写equals方法之前的结果
        System.out.println(t1 == t2);  //false
        //重写equals方法之后的结果
        MyTime02 t3 = new MyTime02(2021, 02, 13);
        System.out.println(t1.equals(t3));
    }
}
class MyTime02 {
    
    
    //定义属性
    private int year;
    private int month;
    private int day;
    //无参构造
    public MyTime02() {
    
    }
    //有参构造
    public MyTime02(int year, int month, int day) {
    
    
        this.year = year;
        this.month = month;
        this.day = day;
    }
    public int getYear() {
    
    
        return year;
    }
    public void setYear(int year) {
    
    
        this.year = year;
    }
    public int getMonth() {
    
    
        return month;
    }
    public void setMonth(int month) {
    
    
        this.month = month;
    }
    public int getDay() {
    
    
        return day;
    }
    public void setDay(int day) {
    
    
        this.day = day;
    }
    public String toString() {
    
    
        return this.year + "/" + this.month + "/" + this.day;
    }

    //重写object类中的equals方法
    //怎么重写,直接复制粘贴object类中的equals方法
/*
    public boolean equals(Object obj) {
        //当年月日相同时,表示两个日期相同,两个对象相等
        //获取第一个日期的年月日
        int year1 = this.year;
        int month1 = this.month;
        int day1 = this.day;
        //获取第二个日期的年月日
        if (obj instanceof MyTime02){
            MyTime02 t = (MyTime02)obj;
            int year2 = t.year;
            int month2 = t.month;
            int day2 = t.day;
            if (year1 == year2 && month1 == month2 && day1 == day2){
                return true;
            }
        }
        return false;
    }
    */
    //改良版,重写equals方法
    public boolean equals(Object obj) {
    
    
        if (obj == null || !(obj instanceof MyTime02)){
    
    
            return false;
        }
        if (this == obj){
    
    
            return true;
        }
        MyTime02 t = (MyTime02)obj;
        return this.year == t.year && this.month == t.month && this.day == t.day;
    }
}

输出结果:
true
2021/2/13
false
true
代码演示3:

public class Aboutequals02 {
    
    
    public static void main(String[] args) {
    
    
        Student s1 = new Student(1001011, "二中");
        Student s2 = new Student(1001011, "二中");
        System.out.println(s1.equals(s2));
    }
}
class Student {
    
    
    int no;
    String school;
    public Student(){
    
    }
    public Student(int no, String school){
    
    
        this.no = no;
        this.school = school;
    }
    //重写equals方法
    //需求:当一个学生的学号相等,并且学号相同时,表示同一个学生;
    //equals方法的编写都是固定的。架子差不多
    public boolean equals(Object obj) {
    
    
       if (obj == null || !(obj instanceof Student)) return false;
        if (this == obj) return true;
        Student s = (Student)obj;
        return this.no == s.no && this.school.equals(s.school);
    }
}

输出结果:
true
代码演示4:

public class Aboutequals03 {
    
    
    public static void main(String[] args) {
    
    
       // Address a = new Address("北京", "朝阳区", "1001011");
        User u1 = new User("张三", new Address("北京", "朝阳区", "1001011"));
        User u2 = new User("张三", new Address("北京", "朝阳区", "1001011"));
        System.out.println(u1.equals(u2));
    }
}
class User {
    
    
    String name;
    Address addr;
    public User(){
    
    }
    public User(String name, Address addr){
    
    
        this.name = name;
        this.addr = addr;
    }
    public boolean equals(Object obj) {
    
    
        if (obj == null || !(obj instanceof User)) return false;
        if (this == obj) return true;
        User u = (User)obj;
        return this.name.equals(u.name) && this.addr.equals(u.addr);
    }
}
class Address {
    
    
    String city;
    String street;
    String zipcode;
    public Address() {
    
    }
    public Address(String city, String street, String zipcode) {
    
    
        this.city = city;
        this.street = street;
        this.zipcode = zipcode;
    }
    public boolean equals(Object obj) {
    
    
        if (obj == null || !(obj instanceof Address)) return false;
        if (this == obj) return true;
        Address a = (Address)obj;
        return this.city.equals(a.city) && this.street.equals(a.street) && this.zipcode.equals(a.zipcode);
    }
}

输出结果:
true
课堂笔记2:
/*
java语言当中的字符串String有没有重写toString方法和equals方法?
1.String类中已经重写了equals方法,比较两个字符串不能使用"",必须使用equals。
2.String类已经重写了toString方法。
3.java中基本数据类型比较是否相等时使用"
"。
java中所有引用数据类型统一使用equals方法来判断是否相等。
*/
代码演示5:

 public class AboutReWrite {
    
    
    public static void main(String[] args) {
    
    
        //大部分情况下采用这种方式创建字符串对象
        String s1 = "hello";
        String s2 = "world";
        //实际上String也是一个类,属于引用数据类型
        //String既然是一个类,那么一定存在构造方法
        String s3 = new String("Demo01");
        String s4 = new String("Demo01");
        System.out.println(s3.equals(s4));
    }
}

输出结果:
true
课堂笔记3:
/*
关于object类中的finalize()方法
1.在object类中的源代码:
protected void finalize() throws Throwable { }
2.finalize()方法只有一个方法体,里面没有代码,而且这个方法是protected修饰的。
3.这个方法不需要程序员手动调用,JVM的垃圾回收器负责调用这个方法。
4.finalize()方法的执行时机:
当一个java对象即将被垃圾回收器回收的时候,垃圾回收器负责调用finalize()方法。
5.finalize()方法实际上是SUN公司为java程序员准备的一个时机,垃圾销毁时机。
如果希望在对象销毁时机执行一段代码的话,这段代码要写到finalize()方法当中。
6.java中的垃圾回收器不是轻易启动的,垃圾太少或者时间没到,种种条件下,
有可能启动,也有可能不启动。
*/
代码演示6:

public class Aboutfinalize {
    
    
    public static void main(String[] args) {
    
    
        for(int i = 0; i <= 100000000; i++){
    
    
            ABC a = new ABC();
            a = null;
        }
    }
}
class ABC {
    
    
    //项目开发中有这样的业务需求:所有对象在JVM中被释放的时候,请记录一下释放时间。
    //记录对象被释放的时间点,这个负责记录的代码就写到finalize()方法中。
    protected void finalize() throws Throwable {
    
    
        System.out.println("即将被销毁");
    }
    }

猜你喜欢

转载自blog.csdn.net/qq_55171059/article/details/113803499