Java自学教程之(Object及其部分方法的应用)



Java自学教程之(Object及其部分方法的应用)。Object :所有类的根类
Object是不断抽取而来的,具备着所有对象都具备的关系内容

方法摘要:

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

 

  1. class Man
  2. {
  3.     private int age = 3;
  4.     Man(int age)
  5.     {
  6.         this.age = age;
  7.     }
  8. }
  9. class Son
  10. {
  11. }
  12. public class Main
  13. {
  14.     public static void main(String[] args)
  15.     {
  16.         Man BLF = new Man(20);
  17.         Man BLF1 = new Man(20);
  18.         Man BLF2 = BLF1;
  19.         System.out.println(BLF==BLF1);//false
  20.         System.out.println(BLF.equals(BLF1));//flase
  21.         //为什么是false?
  22.         //一旦一个Man(20)传递年龄,另一个MAN(“xxx”)传递姓名,这两个肯定不一样
  23.         System.out.println(BLF1.equals(BLF2));//true
  24.         Son BLF5 = new Son();
  25.         System.out.print(BLF2.equals(BLF5));//不一样的类定义的对象也可以比较
  26.     }
  27. }
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只判断地址是不够用的,可以复写 一般都会覆盖此方法,根据对象的特有内容,建立判断对象是否相等的依据

 

  1. import java.net.InterfaceAddress;
  2. import javax.management.RuntimeErrorException;
  3. class Man extends Object
  4. {
  5.     private int age = 3;
  6.     Man(int age)
  7.     {
  8.         this.age = age;
  9.     }
  10.     //比较是否同龄
  11.     public boolean cmp(Man b)
  12.     {
  13.         return this.age==b.age;
  14.     }//但是Man继承于Objext,Objext有equals方法,就没必要写cmp这个重复的方法
  15.     //复写父类的equals
  16.     public boolean equals(Object b)//多态,向上转型
  17.     {
  18.         if(!(b instanceof Man))
  19.         {
  20.             //return false;
  21.             //或者警告类型错误
  22.             throw new RuntimeException(“不要故意找茬,只判断人”);
  23.         }
  24.         Man c = (Man)b;//向下转型
  25.         return this.age==c.age;
  26.     }
  27. }
  28. class animol
  29. {
  30. }
  31. public class Main
  32. {
  33.     public static void main(String[] args)
  34.     {
  35.         Man BLF = new Man(20);
  36.         Man BLF2 = new Man(20);
  37.         System.out.println(BLF.cmp(BLF2));
  38.         animol BLF3 = new animol();
  39.         System.out.println(BLF.equals(BLF3));
  40.     }
  41. }
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()//哈希,集合框架中会重要应用

 

  1. import java.net.InterfaceAddress;
  2. import javax.management.RuntimeErrorException;
  3. class Man extends Object
  4. {
  5.     private int age = 3;
  6.     Man(int age)
  7.     {
  8.         this.age = age;
  9.     }
  10. }
  11. class animol
  12. {
  13. }
  14. public class Main
  15. {
  16.     public static void main(String[] args)
  17.     {
  18.         Man BLF = new Man(20);
  19.         Man BLF2 = new Man(20);
  20.         System.out.println(BLF);
  21.         System.out.println(BLF.hashCode());//十进制
  22.         System.out.println(Integer.toHexString(BLF.hashCode()));
  23.     }
  24. }
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就需要复写


 

  1. import java.net.InterfaceAddress;
  2. import javax.management.RuntimeErrorException;
  3. class Man extends Object
  4. {
  5.     private int age = 3;
  6.     Man(int age)
  7.     {
  8.         this.age = age;
  9.     }
  10.     public int hashCode()
  11.     {
  12.         return age;
  13.     }
  14. }
  15. public class Main
  16. {
  17.     public static void main(String[] args)
  18.     {
  19.         Man BLF = new Man(20);
  20.         Man BLF2 = new Man(20);
  21.         System.out.println(BLF);
  22.         System.out.println(BLF.hashCode());//十进制,20的十六进制为14
  23.         System.out.println(Integer.toHexString(BLF.hashCode()));
  24.     }
  25. }
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();每个对象都有自己的所属的字节码文件对象

 

  1. import java.net.InterfaceAddress;
  2. import javax.management.RuntimeErrorException;
  3. class Man
  4. {
  5.     private int age = 3;
  6.     Man(int age)
  7.     {
  8.         this.age = age;
  9.     }
  10.     public int hashCode()
  11.     {
  12.         return age;
  13.     }
  14. }
  15. public class Main
  16. {
  17.     public static void main(String[] args)
  18.     {
  19.         Man BLF = new Man(20);
  20.         Man BLF2 = new Man(20);
  21.         Class c = BLF.getClass();
  22.         Class d = BLF2.getClass();
  23.         System.out.println(c);
  24.         System.out.println(c==d);//true,两个对象的字节码文件对象是相同的
  25.         //Class中有getName方法得到的当前运行时的类名
  26.         System.out.println(c.getName());//Man
  27.     }
  28. }
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方法:

  1. import java.net.InterfaceAddress;
  2. import javax.management.RuntimeErrorException;
  3. class Man
  4. {
  5.     private int age = 3;
  6.     Man(int age)
  7.     {
  8.         this.age = age;
  9.     }
  10. }
  11. public class Main
  12. {
  13.     public static void main(String[] args)
  14.     {
  15.         Man BLF = new Man(20);
  16.         Man BLF2 = new Man(20);
  17.         System.out.println(BLF);//Man@15db9742,@以前是getName,@以后是hashCode得到
  18.             System.out.println(BLF.toString());//实际上上面BLF后面就是省略的.toString()
  19.         System.out.println(BLF.getClass().getName()+”—-”+Integer.toHexString(BLF.hashCode()));
  20.     }
  21. }
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()));	
	}
}

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

 

  1. import java.net.InterfaceAddress;
  2. import javax.management.RuntimeErrorException;
  3. class Man
  4. {
  5.     private int age = 3;
  6.     Man(int age)
  7.     {
  8.         this.age = age;
  9.     }
  10.     public String toString()
  11.     {
  12.         return “Man”+age;
  13.     }
  14. }
  15. public class Main
  16. {
  17.     public static void main(String[] args)
  18.     {
  19.         Man BLF = new Man(20);
  20.         Man BLF2 = new Man(20);
  21.         System.out.println(BLF);//Man20
  22.     }
  23. }
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:还有一些方法,等到渣渣学到多线程是再补全