Java常用类01:Object类

getClass()方法

返回对象存储的实际类型,可用于判断两个对象实际存储的类型是否一致

public class Hello {
    public static void main(String[] args) {
        Test test = new Test();
        test.test();
    }
}

class Test extends Hello{
    public void test(){
        Hello a = new Test();
        Test b = new Test();
        System.out.println(a.getClass() == b.getClass());  //true,虽然a的声明类型是父类,其实际类型仍为子类
        System.out.println(a instanceof Test);  //true
        System.out.println(a.getClass());  //class Test
    }
}

hashCode()方法

返回对象的哈希码值,根据对象的地址或字符串或数字使用hash算法计算出来的int类型数值,相同对象返回相同数值

public class Hello {
    public static void main(String[] args) {
        Hello a = new Hello();
        Hello b = a;
        System.out.println(a.hashCode());
        System.out.println(b.hashCode());  //利用哈希值来判断是不是同一个对象
    }
}

toString()方法

返回对象的字符串表示(类名@哈希值),一般会重写toString()方法自定义打印的内容

public class Hello {
    public static void main(String[] args) {
        System.out.println(new Hello().toString());
    }
    
    @Override
    public String toString() {  //快捷键ctrl + O
        return "重写toString()方法";
    }
}

equals()方法

判断两个对象或字符串是否相等,可重写equals()方法更改判断的规则

public class Hello {
    public static void main(String[] args) {
        Test a = new Test(1, 2);
        Test b = new Test(1, 2);  //在重写了equals()方法后,如果两个对象的属性值相同,也算同一对象
        System.out.println(a.equals(b));
    }
}

class Test{
    int a;
    int b;
    public Test(){
    }
    
    public Test(int a, int b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj){  //1.比较两个引用是否指向同一个对象
            return true;
        }
        
        else if (obj == null){  //2.判断对象是否为null
            return false;
        }
        
        else if (obj instanceof Test){  //3.判断两个引用指向的实际对象类型是否一致
            Test n = (Test) obj;  //4.强制类型转换,因为在instanceof判断中,子类也是父类的对象类型,所以需要强制转换确保类型为Test
            if (this.a == n.a && this.b == n.b){  //5.依次比较各个属性值是否相同
                return true;
            }
        }
        return false;
    }
}

finalize()方法

JVM自动调用此方法,进行垃圾对象回收

clone()方法

使用clone()方法需要实现Cloneable接口,并抛出异常

该方法是浅拷贝,如果类有引用属性(比如字符串或者其他类的对象),则两个对象的该属性指向同一个地址,并不是独立的

想实现深拷贝,或者自定义克隆规则,需要重写clone()方法

//默认(浅拷贝)
public class Hello implements Cloneable{
    Test test;

    public Hello(Test test) {
        this.test = test;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Hello a = new Hello(new Test());
        Hello b = a;
        //clone()方法是Objec类的,所以生成的对象也是Object,需要强制转换
        Hello c = (Hello) a.clone();

        System.out.println(a);  //Hello@1b6d3586
        System.out.println(b);  //Hello@1b6d3586,直接赋值得到的是同一个对象
        System.out.println(c);  //Hello@4554617c,clone()方法得到的是不同的对象
        System.out.println(a.test.equals(c.test));  //true,Hello有一个引用类型的属性,但是a和c的test却是同一个Test对象,说明只是拷贝了这个属性的引用地址,这是浅拷贝
    }
}

class Test{}


//深拷贝
public class Hello implements Cloneable{
    Test test;

    public Hello(Test test) {
        this.test = test;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Hello a = new Hello(new Test());
        Hello b = a;
        Hello c = (Hello) a.clone();

        System.out.println(a.test.equals(c.test));  //false,说明a和c的引用属性也独立了
    }
	
    //深拷贝需要重写clone()方法,将test属性单独克隆
    @Override
    protected Object clone() throws CloneNotSupportedException {
        //1.定义一个Hello对象,由Object类的克隆对象转换而来
        Hello hello = (Hello) super.clone();
        //2.将这个Hello对象的test属性单独克隆。注意此处的test.clone(),该clone()方法是Test类重写的
        hello.test = (Test) test.clone();
        //3.返回这个新的Hello对象
        return hello;
    }
}

//重写clone()方法也要实现Cloneable接口
class Test implements Cloneable {
	
    //clone()方法是protected修饰的,在其他类不能通过创建Test类的对象来调用,所以必须重写clone()方法
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

notify()和wait()方法

见多线程部分

posted @ 2021-09-08 18:23  振袖秋枫问红叶  阅读(56)  评论(0)    收藏  举报