方法摘要:
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()方法,或者其他某个线程中断当前线程,或者已超过某个实际时间量前,导致当前线程等待
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));//不一样的类定义的对象也可以比较
}
}
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));
}
}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()));
}
}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()));
}
}
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
}
}
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
}
}
原文:http://blog.csdn.net/wjw0130/article/details/39588143