当前位置:   article > 正文

9、 Object类中toString方法、equals方法、finalize方法、hashCode方法_idea重写finalize

idea重写finalize

API

应用程序编程接口,整个JDK的类库就是一个javase的API。
每一个API都会配置一套API帮助文档

toString方法

1、源码

public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
  • 1
  • 2
  • 3

源代码上toString()方法的默认实现是:
类名@对象的内存地址转换为十六进制的形式
2、toString()方法的作用是什么?
toString()方法设计的目的:通过调用这个方法可以将一个j"ava对象"转换为"字符串"。
3、SUN公司开发Java语言的时候,建议所有的子类都去重写toString()方法。
toString()方法应该是一个简洁的、详实的、易阅读的。

//不重写toString()方法,输出的结果是类名@十六进制
public class Tedt01 {
    public static void main(String[] args) {
        MyTime m = new MyTime(1928,11,27);
        String s1 = m.toString();
        System.out.println(s1);
        //MyTime@7f31245a
    }
}

class MyTime{
    int year;
    int month;
    int day;

    public MyTime() {
    }

    public MyTime(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 + "日";
    }
    //进行方法重写之后输出:1928年11月27日
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

equals方法

1、源码

public boolean equals(Object obj) {
        return (this == obj);
    }
  • 1
  • 2
  • 3

2、SUN公司设计equals方法的目的:通过equals方法判断两个对象是否相等。
3、我们会发现equals方法不够用!!
在Object中的equals方法中,默认使用的是 两个等号来判断两个java对象是否相等。
4、判断两个对象是否相等不能使用"==",因为等号比较的是两个对象的内存地址。我们相判断的是两个类的值是否相等。
所以我们需要重写equals方法。

equals如何对比两个对象是否一样?

package com.bjpowernode.javase.day16.homework;

public class Tedt01 {
    public static void main(String[] args) {
        MyTime m = new MyTime(1928,11,27);
        MyTime f = new MyTime(1928,11,27);
        boolean flag = m.equals(f);
        System.out.println(flag);


    }
}

class MyTime{
    int year;
    int month;
    int day;

    public MyTime() {

    }

    public MyTime(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }
    //重写equals方法
    public boolean equals(Object obj){
        //年相同,月相同,日相同,则两个对象相同
        int year1 = this.year;
        int month1 = this.month;
        int day1 = this.day;

        if (obj instanceof MyTime){
            MyTime t = (MyTime)obj;
            int year2 = t.year;
            int month2 = t.month;
            int day2 = t.day;
            if (year1 == year2 && month2 == month2 && day1 == day2) {
                return true;
            }
        }
        return false;
    }


}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

对代码进行优化之后的equals方法:

public boolean equals(Object obj){
        if( ! (obj instanceof MyTime) || obj == null)
            return false;
        if (this == obj )
            return true;
        MyTime m = (MyTime) obj;
        //if (this.month == m.month && this.year == m.year && this.day == m.day){
        return (this.month == m.month && this.year == m.year && this.day == m.day);
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

idea一键生成的equals方法:

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyTime myTime = (MyTime) o;
        return year == myTime.year && month == myTime.month && day == myTime.day;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

那么String类有没有调用equals方法了?
1、String类已经重写了equals方法,比较两个字符串不能使用==,必须使用equals。equals是通用的。
2、String类已经重写了toString方法。

结论:
java中什么类型的数据可以使用两个等号判断
java中基本数据类型比较是否相等,使用==
java中什么类型的数据可以使用equals判断
java中所有的引用数据类型统一使用equals方法来判断是否相等

关于equals方法重写的练习:

package com.bjpowernode.javase.day16.work;

public class Test01 {

    public static void main(String[] args) {
        User u1 = new User("001",new Address("zhongguo","dalong","43hao"));
        User u2 = new User("001",new Address("zhongguo","dalong","43hao"));
        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;
    }
    //重写equals方法
    public boolean equals(Object o){
        if (this == o ) return true;
        if(o == null || !(o instanceof User)) return false;
        User u = (User)o;
        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 String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getZipcode() {
        return zipcode;
    }

    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    //重写equals方法
    public boolean equals(Object o){
        if (o == null || !(o instanceof Address)) return false;
        if (this == o) return true;
        Address a = (Address) o;
        if (this.city.equals(a.city)
                && this.street.equals(a.street)
                && this.zipcode.equals(a.zipcode)) return true;
        else return false;

    }
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

finalize方法

1、源码

protected void finalize() throws Throwable { }
  • 1

GC负责调用finalize()方法
2、finalize()方法只有一个方法体,里面没有代码,而且这个方法是protected修饰的。
3、这个方法不需要程序员手动调用,JVM的垃圾回收器负责调用这个方法。
4、finalize()方法的执行时机:
当一个java对象即将被垃圾回收器回收的时候,垃圾回收器负责调用这个方法。
equals、toString方法需要程序员负责调用这个方法,finalize只需要重写,重写完将来会自动有程序调用。
5、finalize()方法实际上是SUN公司为java程序员准备的一个时机,垃圾销毁时机。如果希望在对象销毁时机执行一段代码的话,这段代码要写到finalize()方法当中。
6、类似于静态代码块:
static{

}
静态代码块在类加载时刻执行,并且只执行一次。
这是SUN准备的类加载时机


hashCode方法

1、源码:

public native int hashCode();
  • 1

这个方法不是抽象方法,带有native关键字,底层调用的是C++程序。
2、hashCode方法返回的是哈希值:
实际上就是java对象的内存地址,经过哈希算法,得出的一个值。
所以hashCode方法的执行结果可以等同看成一个java对象的内存地址。
3、

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/431206
推荐阅读
相关标签
  

闽ICP备14008679号