ObjectObjects

Posted 征程

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ObjectObjects相关的知识,希望对你有一定的参考价值。

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.

 

以上是关于ObjectObjects的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序代码片段

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器