码迷,mamicode.com
首页 > 其他好文 > 详细

Object、Objects

时间:2017-08-02 23:23:28      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:log   add   cto   support   int   creat   prim   false   bad   

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.

 

Object、Objects

标签:log   add   cto   support   int   creat   prim   false   bad   

原文地址:http://www.cnblogs.com/YKang/p/7277139.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!