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++程序
目的
通过调用哈希算法,将一个对象的内存地址转换成一个数字,可以看做是一个对象的地址
本文详细介绍了Java中Object类的几个关键方法:toString()用于将对象转化为字符串,通常需要子类重写以提供更有意义的输出;equals()方法默认比较对象引用,需要根据需求重写以实现对象内容的比较;finalize()是对象销毁前的回调方法,通常用于资源清理;hashCode()返回对象的哈希值,常用于哈希表操作。文章通过示例代码解释了这些方法的默认行为及如何在自定义类中进行适配。
——day07Object类&spm=1001.2101.3001.5002&articleId=117134280&d=1&t=3&u=96471e934e324002b7467ebdcb11e6b4)
2176

被折叠的 条评论
为什么被折叠?



