博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java核心技术----Object类
阅读量:5462 次
发布时间:2019-06-15

本文共 6370 字,大约阅读时间需要 21 分钟。

package java.lang;/** * Class {@code Object} is the root of the class hierarchy. * Every class has {@code Object} as a superclass. All objects, * including arrays, implement the methods of this class. * * @author  unascribed * @see     java.lang.Class * @since   JDK1.0 */public class Object {    private static native void registerNatives();    static {        registerNatives();    }        public final native Class
getClass(); public native int hashCode(); public boolean equals(Object obj) { return (this == obj); } protected native Object clone() throws CloneNotSupportedException; public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()); } public final native void notify(); public final native void notifyAll(); public final native void wait(long timeout) throws InterruptedException; public final void wait(long timeout, int nanos) throws InterruptedException { if (timeout < 0) { throw new IllegalArgumentException("timeout value is negative"); } if (nanos < 0 || nanos > 999999) { throw new IllegalArgumentException( "nanosecond timeout value out of range"); } if (nanos > 0) { timeout++; } wait(timeout); } public final void wait() throws InterruptedException { wait(0); } protected void finalize() throws Throwable { }}

Object类是Java中所有类的始祖,在Java中每个类都是由它扩展而来。

在Java中,只有基本类型不是对象(数组也都扩展了Object类)。

/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类是不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone  */class Person{	private int age;	public Person(int age){		this.age = age;	}}public class test {    public static void main(String... args) {        Person p1 = new Person(20);        Person p2 = new Person(20);        System.out.println(p1 == p2);        System.out.println(p1.equals(p2));    }}

输出:

false

false

equals比较的是两个对象是否指向同一个位置

重写equals方法:

/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone  */class Person{	private int age;	public Person(int age){		this.age = age;	}	//根据Person类的年龄进行比较	@Override	public boolean equals(Object obj){		if (!(obj instanceof Person)) {			throw new ClassCastException("类型错误");		}		Person p = (Person)obj;		return (age == p.age);	}}public class test {    public static void main(String... args) {        Person p1 = new Person(20);        Person p2 = new Person(21);        Person p3 = new Person(20);        System.out.println(p1 == p2);        System.out.println(p1.equals(p2));        System.out.println(p1.equals(p3));    }}

输出:

false

false
true

/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone  */class Person{	private int age;	public Person(int age){		this.age = age;	}	//根据Person类的年龄进行比较	@Override	public boolean equals(Object obj){		if (!(obj instanceof Person)) {			throw new ClassCastException("类型错误");		}		Person p = (Person)obj;		return (age == p.age);	}}public class test {    public static void main(String... args) {        Person p1 = new Person(20);        Person p2 = new Person(21);        Person p3 = new Person(20);        Demo demo = new Demo();        System.out.println(p1 == p2);        System.out.println(p1.equals(p2));        System.out.println(p1.equals(p3));        System.out.println("p1 = " + p1);        System.out.println("p1.hashCode() = " + p1.hashCode());        System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode()));    }}

输出:

false
false
true
p1 = Person@28d93b30
p1.hashCode() = 685325104
Integer.toHexString(p1.hashCode()) = 28d93b30
一般重写equals方法后都需要重写HashCode,因为相等的两个对象必须确保hashCode相等。

/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone  */class Person{	private int age;	public Person(int age){		this.age = age;	}	//根据Person类的年龄进行比较	@Override	public boolean equals(Object obj){		if (!(obj instanceof Person)) {			throw new ClassCastException("类型错误");		}		Person p = (Person)obj;		return (age == p.age);	}	@Override	public int hashCode(){		return age;	}}public class test {    public static void main(String... args) {        Person p1 = new Person(20);        Person p2 = new Person(21);        Person p3 = new Person(20);        Demo demo = new Demo();        System.out.println(p1 == p2);        System.out.println(p1.equals(p2));        System.out.println(p1.equals(p3));        System.out.println("p1 = " + p1);        System.out.println("p1.hashCode() = " + p1.hashCode());        System.out.println("Integer.toHexString(p1.hashCode()) = " + Integer.toHexString(p1.hashCode()));    }}

/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone  */class Person{	private int age;	public Person(int age){		this.age = age;	}	//根据Person类的年龄进行比较	@Override	public boolean equals(Object obj){		if (!(obj instanceof Person)) {			throw new ClassCastException("类型错误");		}		Person p = (Person)obj;		return (age == p.age);	}/*	@Override	public int hashCode(){		return age;	}*/}public class test {    public static void main(String... args) {        Person p1 = new Person(20);       	System.out.println(p1);       	System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode()));    }}
重写toString方法:

/** * Created by N3verL4nd on 2016/12/4. * Object类:所有类的超类 * Object类似不断抽取而来,具备着所有对象都具备的共性内容。 * 常用的共性内容: * equals toString hashCode clone  */class Person{	private int age;	public Person(int age){		this.age = age;	}	//根据Person类的年龄进行比较	@Override	public boolean equals(Object obj){		if (!(obj instanceof Person)) {			throw new ClassCastException("类型错误");		}		Person p = (Person)obj;		return (age == p.age);	}/*	@Override	public int hashCode(){		return age;	}*/	public String toString(){		return "Person@@" + age;	}}public class test {    public static void main(String... args) {        Person p1 = new Person(20);       	System.out.println(p1);       	System.out.println(p1.getClass().getName() + "@" + Integer.toHexString(p1.hashCode()));    }}

输出:

Person@@20

Person@28d93b30

转载于:https://www.cnblogs.com/lgh1992314/p/6616274.html

你可能感兴趣的文章
SCOI2009游戏 (数论+dp)
查看>>
tcpdump 学习(3):MySQL Query
查看>>
20145326蔡馨熤《网络对抗》—— Web安全基础实践
查看>>
linux 安装pyodbc
查看>>
PowerBuilder通过ODBC和JBDC连接mysql数据库
查看>>
ORACLE数据库 memory_target SGA 大小
查看>>
JavaScript中的null和undefined
查看>>
linux清空屏幕
查看>>
python中的位运算符
查看>>
Java关键字之native,strictfp,transient ,volatile
查看>>
重拾C,一天一点点_2
查看>>
鼠标事件
查看>>
爬取工大新闻并发送到邮箱中
查看>>
VMware虚拟机中Hadoop服务的端口无法访问的问题
查看>>
我所理解的产品经理
查看>>
OBIEE + OAS集群配置 Part 1
查看>>
pepperflash
查看>>
[树莓派]安装node环境
查看>>
BZOJ3555 CTSC2014 企鹅QQ HASH+二分法
查看>>
Xcode证书文件存储目录
查看>>