1. 程式人生 > >Class、Object類

Class、Object類

Class類

    

/**
 * Returns the  name of the entity (class, interface, array class,
 * primitive type, or void) represented by this {@code Class} object,
 * as a {@code String}.
 */
public String getName() {
    String name = this.name;
    if (name == null)
        this.name = name = getName0();
    return name;
}

 

==和equals()的區別:

        ==: 基本型別,比較的是值是否相同;引用型別,比較的是地址值是否相同

        equals(): 只能比較引用型別,預設情況下,比較的是地址值是否相同;重寫後比較的是物件中的值

Object類

 

public class A implements Cloneable {

    private String content;

    private String time;

    public A() {
    }

    public A(String content, String time) {
        this.content = content;
        this.time = time;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        A a = (A) o;
        return Objects.equals(content, a.content) &&
                Objects.equals(time, a.time);
    }

    @Override
    public int hashCode() {
        return Objects.hash(content, time);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "A{" +
                "content='" + content + '\'' +
                ", time='" + time + '\'' +
                '}';
    }
}
public class ATest {
    public static void main(String[] args) {
        A a = new A("開發", "8");

        try {
            Object cloneA = a.clone();
            // A{content='開發', time='8'}
            System.out.println(cloneA);
            A oldA = a;
            oldA.setContent("分類");
            oldA.setTime("6");
            // A{content='分類', time='6'}
            System.out.println(a);
            // A{content='分類', time='6'}
            System.out.println(oldA);
            // A{content='開發', time='8'}
            System.out.println(cloneA);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

 

Object 克隆是淺克隆

class B {
    private String play;

    public B() {
    }

    public B(String play) {
        this.play = play;
    }

    public String getPlay() {
        return play;
    }

    public void setPlay(String play) {
        this.play = play;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        B b = (B) o;
        return Objects.equals(play, b.play);
    }

    @Override
    public int hashCode() {
        return Objects.hash(play);
    }

    @Override
    public String toString() {
        return "B{" +
                "play='" + play + '\'' +
                '}';
    }
}
public class A implements Cloneable {

    private B b;
    private String content;

    private String time;

    public A() {
    }

    public A(B b, String content, String time) {
        this.b = b;
        this.content = content;
        this.time = time;
    }

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        A a = (A) o;
        return Objects.equals(b, a.b) &&
                Objects.equals(content, a.content) &&
                Objects.equals(time, a.time);
    }

    @Override
    public int hashCode() {
        return Objects.hash(b, content, time);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "A{" +
                "b=" + b +
                ", content='" + content + '\'' +
                ", time='" + time + '\'' +
                '}';
    }
}
public class ATest {
    public static void main(String[] args) {
        B b = new B("Game");
        A a = new A(b,"開發", "8");

        try {
            A cloneA = (A)a.clone();
            // false
            System.out.println(a == cloneA);
            // true
            System.out.println(b == cloneA.getB());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}

深度克隆

class B implements Cloneable{
    private String play;

    public B() {
    }

    public B(String play) {
        this.play = play;
    }

    public String getPlay() {
        return play;
    }

    public void setPlay(String play) {
        this.play = play;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        B b = (B) o;
        return Objects.equals(play, b.play);
    }

    @Override
    public int hashCode() {
        return Objects.hash(play);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "B{" +
                "play='" + play + '\'' +
                '}';
    }
}
public class A implements Cloneable {

    private B b;
    private String content;

    private String time;

    public A() {
    }

    public A(B b, String content, String time) {
        this.b = b;
        this.content = content;
        this.time = time;
    }

    public B getB() {
        return b;
    }

    public void setB(B b) {
        this.b = b;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        A a = (A) o;
        return Objects.equals(b, a.b) &&
                Objects.equals(content, a.content) &&
                Objects.equals(time, a.time);
    }

    @Override
    public int hashCode() {
        return Objects.hash(b, content, time);
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        A clone = (A)super.clone();
        // 引用物件也拷貝一份
        clone.b = (B) b.clone();
        return clone;
    }

    @Override
    public String toString() {
        return "A{" +
                "b=" + b +
                ", content='" + content + '\'' +
                ", time='" + time + '\'' +
                '}';
    }
}
public class ATest {
    public static void main(String[] args) {
        B b = new B("Game");
        A a = new A(b,"開發", "8");

        try {
            A cloneA = (A)a.clone();
            // false
            System.out.println(a == cloneA);
            // false
            System.out.println(b == cloneA.getB());
            // B{play='Game'}
            System.out.println(b);
            // B{play='Game'}
            System.out.println(cloneA.getB());
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }