• Object、Objects


    Constructor 

    Object()

    Method

    Object clone()   // protected shallow copy ,override it as public if the subclass use this method  ,

        // implement the  Cloneable interface,just a mark

        Creates and returns a copy of this object. 

    private String name;
        private int age;
        private int arr[];
        private Heart h;
        
        @Override
        public Object clone() throws CloneNotSupportedException {
            return super.clone();
        }
    
    
        
            People p = new People();    
            People p2 = (People)p.clone();   //Object 类实现的是浅拷贝,但是能对String,数组进行深拷贝
            System.out.println(p);            // H是一个类,引用类型,此时可看出进行了浅拷贝
            System.out.println(p2);
            
            p2.setAge(100);
            p2.setName("hah");
            int[] arr = {1,1,1};
            p2.setArr(arr);
            p2.getH().setHealth("bad");
            p2.getH().setJump(100);
            
            System.out.println("++++++++++++++++++++");
            System.out.println(p2);
            System.out.println(p);
            
    //        People [name=alex, age=0, arr=[1, 2, 3], h=Heart [jump=80, health=good]]
    //        People [name=alex, age=0, arr=[1, 2, 3], h=Heart [jump=80, health=good]]
    //                ++++++++++++++++++++
    //        People [name=hah, age=100, arr=[1, 1, 1], h=Heart [jump=100, health=bad]]
    //        People [name=alex, age=0, arr=[1, 2, 3], h=Heart [jump=100, health=bad]]
            
    View Code
    public People clone() throws CloneNotSupportedException {
    
            People p = new People();
            return p;
        }
    public People()
        {
            this("alex",0,new int[]{1,2,3}, new Heart(80, "good"));
        }
    public People(String name, int age, int[] arr, Heart h)
        {
            super();
            this.name = name;
            this.age = age;
            this.arr = arr;
            this.h = h;
        }
    
    People [name=alex, age=0, arr=[1, 2, 3], h=Heart [jump=80, health=good]]
    People [name=alex, age=0, arr=[1, 2, 3], h=Heart [jump=80, health=good]]
    ++++++++++++++++++++
    People [name=hah, age=100, arr=[1, 1, 1], h=Heart [jump=100, health=bad]]
    People [name=alex, age=0, arr=[1, 2, 3], h=Heart [jump=80, health=good]]
    View Code

    boolean equals()  //  return true if  their address is equals , which isn't very useful

        Indicates whether some other object is "equal to" this one.

    public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            People other = (People) obj;
            if (age != other.age)
                return false;
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
    
    等价于
    public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            People other = (People) obj;
            
            return Objects.equals(this.name, other.name)&&
                    this.age == other.age ;
        }
    View Code

    Class<?>  getClass()

        Returns the runtime class of this Object.

    int hashCode()   //  the Object get the hash code according to the address

        Returns a hash code value for the object

    @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + age;
            result = prime * result + ((name == null) ? 0 : name.hashCode());
            return result;
        }
    等价于
    
    public int hashCode() {
            
            return Objects.hash(this.age, this.name);
        }
        
    View Code

    String toString()   // the Object implements it by return the memory address

        Returns a string representation of the object.

    public String toString() {
            return "People [name=" + name + ", age=" + age + "]";
        }
    View Code

    all the code above is the implement of subclass

    java.util.Objects  // the tools class for Object

    boolean equals(Object a, Object b)    //  a,b can be null, which is important !

        Returns true if the arguments are equal to each other(both null ) and false otherwise.

    int hash(Object...values)   // you need this when override the hashCode() method, see the code above

        Generates a hash code for a sequence of input values.

    WE ARE ALL IN THE GUTTER, BUT SOME OF US ARE LOOKING AT THE STARS
  • 相关阅读:
    【转载】Python未来互联网主流语言! . 天高地厚
    Android系统Intent的使用(转)
    android:获取联系人信息(姓名和电话)
    CMNET和CMWAP区别(转)
    Android界面开发推荐颜色
    android : framelayout 研究
    android : drag and drop ui
    android 四种模式研究之一
    ListView与CheckBox,EditText,Button结合
    android 之 custom view(一)
  • 原文地址:https://www.cnblogs.com/YKang/p/7277139.html
Copyright © 2020-2023  润新知