总结java学习(B站狂神说和动力节点老杜)——day07Object类

Object类

object 中的类是所有类的父类,object中的所有的方法子类都能使用

toString()方法

源代码
public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

默认实现:类名@地址的16进制

getClass().getName():获取当前类的类名

Integer.toHexString(hashCode():将对象的内存地址转换成16进制

目的

通过调用toStrning()方法,将一个"java对象"转换成"字符串表示形式"

public class Test{
    public static void main(String[] args){
        MyTime t1 = new MyTime(1970, 12, 5);
        ti.toString = t1.toString();//MyTime@28a418fc
    }
}
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;
    }
}
/*输出的是t1对象的内存地址*/

SUN公司建议所有子类都去重写toString()方法;

注意:直接输出引用的时候会自动调用toString 方法

MyTime t1 = new MyTime(1970, 12, 5);
System.out.println(t1)//相当于System.out.println(t1。toString());
重写
public class Test{
    public static void main(String[] args){
        MyTime t1 = new MyTime(1970, 12, 5);
        ti.toString = t1.toString();//MyTime@28a418fc
    }
}
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;
    }
    public String toString() {
    	return this.year + "年" + this.month + "月" + this.day + "日";
	}
}
/*返回结果:1970年12月5日*/

equals()方法

源代码
public boolean equals(Object obj) {
    return (this == obj);
}
目的

判断两个对象是否相等,有比较大的局限性,很难直接使用"=="

public class Test{
    public static void main(String[] args){
        MyTime t1 = new MyTime(1970, 12, 5);
        MyTime t2 = new MyTime(1970, 12, 5);
        boolean b = t1.equals(t2);//false
    }
}
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;
    }
}
/*不能直接判断t1对象和t2对象相等*/
初步重写
public class Test{
    public static void main(String[] args){
        MyTime t1 = new MyTime(1970, 12, 5);
        MyTime t2 = new MyTime(1970, 12, 5);
        boolean b = t1.equals(t2);
    }
}
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;
    }
    public boolean equals(Object obj) {
        int year1 = this.year;
        int month1 = this.month;
        int day1 = this.day;
        if(obj instanceof MyTime){
            MyTime t3 = (MyTime) obj;
            if(t3.year == yrea && t3.month == month && t3.day == day){
                return true;
            }
        }
        return false;
	}
}
/*返回结果:true*/
最终重写
public class Test{
    public static void main(String[] args){
        MyTime t1 = new MyTime(1970, 12, 5);
        MyTime t2 = new MyTime(1970, 12, 5);
        System.out.println(t1.equals(t2));//true
    }
}
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;
    }
    public boolean equals(Object obj) {
		if(this == obj) return true;
        if(obj == null || this.getClass() != obj.getClass()) return false;
        MyTime mytime = (MyTime) obj;
        return year == mytime.year && month == mytime.month && day == mytime.day;
	}
}
/*返回结果:true*/

finalize() 方法

源代码
protected void finalize() throws Throwable { }
目的

不需要程序员手动调用,JVM的垃圾回收机制负责调用该方法,作用是在即将销毁一个对象时,为程序员准备的一个时机,类似于静态代码块

hashCode()方法

源代码
public native int hashCode();

该方法使用native修饰,调用底层C++程序

目的

通过调用哈希算法,将一个对象的内存地址转换成一个数字,可以看做是一个对象的地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值