• java核心技术----Object类


    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




    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    Salesforce LWC学习(二十五) Jest Test
    Salesforce LWC学习(二十四) Array.sort 浅谈
    Salesforce LWC学习(二十三) Lightning Message Service 浅谈
    Salesforce LWC学习(二十二) 简单知识总结篇二
    开发第一个基于PyQt5的桌面应用
    python操作sqlite的小例子
    git rebase -i的时候用的不是 vi 编辑器是 nano编辑器不会用
    周六和女友简单的分析了一下飞机大战游戏的设计思路和概念
    Debian/Ubuntu添加PPA源更新提示无公钥被禁用
    布鲁斯口琴进阶教程资料分享
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616273.html
Copyright © 2020-2023  润新知