首页 > 代码库 > Java学习第十七课(Object及其部分方法的应用)

Java学习第十七课(Object及其部分方法的应用)

Object :所有类的根类

Object是不断抽取而来的,具备着所有对象都具备的关系内容

方法摘要:

clone():创建并返回一个此对象的副本
equals(Object obj):指示其他对象是否与此对象“相等”
finalize():当垃圾回收器确定不存在对该对象更多的引用时,由对象的垃圾回收器调用此方法
getClass():返回Objext的运行时类
hashCode():返回该对象的哈希码值
notify():唤醒在此对象监视器上等待的单个线程
notifyAll():唤醒在此对象监视器上等待的所有线程
toString():返回该对象的字符串表示
wait():在其他线程调用此对象的notify()或notifyAll()方法前,导致当前线程等待
wait(long timeout):在其他线程调用此对象的notify()或notifyAll()方法,或者超过指定的时间量前,导致当前线程等待
wait(long timeout,int nanos):在其他线程调用此对象的notify()或notifyAll()方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待

一、equals():(重点掌握)

class Man
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}
}
class Son
{
	
}
public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF1 = new Man(20);
		Man BLF2 = BLF1;
		System.out.println(BLF==BLF1);//false
		System.out.println(BLF.equals(BLF1));//flase
		//为什么是false?
		//一旦一个Man(20)传递年龄,另一个MAN(“xxx”)传递姓名,这两个肯定不一样
		System.out.println(BLF1.equals(BLF2));//true
		Son BLF5 = new Son();
		System.out.print(BLF2.equals(BLF5));//不一样的类定义的对象也可以比较
	}
}

当然很多时候,equals只判断地址是不够用的,可以复写
一般都会覆盖此方法,根据对象的特有内容,建立判断对象是否相等的依据

import java.net.InterfaceAddress;

import javax.management.RuntimeErrorException;


class Man extends Object
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}
	//比较是否同龄
	public boolean cmp(Man b)
	{
		return this.age==b.age;
	}//但是Man继承于Objext,Objext有equals方法,就没必要写cmp这个重复的方法
	//复写父类的equals
	public boolean equals(Object b)//多态,向上转型
	{
		if(!(b instanceof Man))
		{
			//return false;
			//或者警告类型错误
			throw new RuntimeException("不要故意找茬,只判断人");
		}
		Man c = (Man)b;//向下转型
		return this.age==c.age;
	}
}
class animol
{
	
}
public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF2 = new Man(20);
		System.out.println(BLF.cmp(BLF2));
		animol BLF3 = new animol();
		
		System.out.println(BLF.equals(BLF3));
		
	}
}



二、hashCode()//哈希,集合框架中会重要应用

import java.net.InterfaceAddress;
import javax.management.RuntimeErrorException;

class Man extends Object
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}
}
class animol
{
	
}
public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF2 = new Man(20);
		System.out.println(BLF);
		System.out.println(BLF.hashCode());//十进制
		System.out.println(Integer.toHexString(BLF.hashCode()));
	}
}

在实际开发中,可能想要自己定义对象的哈希值,那么hashCode就需要复写

import java.net.InterfaceAddress;
import javax.management.RuntimeErrorException;

class Man extends Object
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}
	public int hashCode()
	{
		return age;
	}
}

public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF2 = new Man(20);
		System.out.println(BLF);
		System.out.println(BLF.hashCode());//十进制,20的十六进制为14
		System.out.println(Integer.toHexString(BLF.hashCode()));
	}
}



三、getClass方法

在创建对象的时候,先在内存加载 Man.class字节码文件对象,然后new Man();每个对象都有自己的所属的字节码文件对象

import java.net.InterfaceAddress;
import javax.management.RuntimeErrorException;

class Man 
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}
	public int hashCode()
	{
		return age;
	}
}

public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF2 = new Man(20);
		Class c = BLF.getClass();
		Class d = BLF2.getClass();
		System.out.println(c);
		System.out.println(c==d);//true,两个对象的字节码文件对象是相同的
		//Class中有getName方法得到的当前运行时的类名
		System.out.println(c.getName());//Man
	}
}


四、toString方法:


import java.net.InterfaceAddress;
import javax.management.RuntimeErrorException;

class Man 
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}

}

public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF2 = new Man(20);
		
		System.out.println(BLF);//Man@15db9742,@以前是getName,@以后是hashCode得到
			System.out.println(BLF.toString());//实际上上面BLF后面就是省略的.toString()
		System.out.println(BLF.getClass().getName()+"----"+Integer.toHexString(BLF.hashCode()));	
	}
}

建议所有子类都重写该方法

import java.net.InterfaceAddress;
import javax.management.RuntimeErrorException;


class Man 
{
	private int age = 3;
	Man(int age)
	{
		this.age = age;
	}
	public String toString()
	{
		return "Man"+age;
	}
}
public class Main
{
	public static void main(String[] args)
	{
		Man BLF = new Man(20);
		Man BLF2 = new Man(20);
		
		System.out.println(BLF);//Man20
	}
}

PS:还有一些方法,等到渣渣学到多线程是再补全

Java学习第十七课(Object及其部分方法的应用)