Object类

Obect是所有类的父类

使用类

package Reference_class;

public class Person  implements Cloneable{
//Cloneable为一个标志性接口,实现该接口的对象可实现自我克隆,接口中没有定义任何方法

    private static Person p =null;
    private int age;
    private double height;
    private String name;

    public Person(){}
    public Person(int age,double height){
        this.age=age;
        this.height=height;
    }
    public Person(int age,double height,String name){
        this.age=age;
        this.height=height;
        this.name = Objects.requireNonNull(name);//name若为null将引发异常,否则将值赋给name
    }

    public void info(){System.out.println("我的年龄是:"+age+",我的身高是:"+height);}

    --------------Getter与Setter方法省略------------------

    //重写equals方法(引用类型及值一致)
    //equals为object类的方法,需重写。getclass()为Object的方法。获取实例变量的引用类型
    public  boolean equals(Object obj){

        if(this == obj){
            return true;
        }
        if(obj.getClass() == Person.class){
            Person to_person = (Person) obj;
            if(this.getAge()==to_person.getAge() &&
                    this.getHeight()==to_person.getHeight()){
                return true;
            }
        }       
        return false;
    }

    //重写finalize()方法,让ft引用到试图回收的可回收方法
    public void finalize(){
        p = this;
    }

    //重写toString()方法
    public String toString(){
        return "年龄为:"+String.valueOf(this.getAge())+",身高为:"+String.valueOf(this.getHeight());
    }
}

     //重写clone方法
     //只是一个浅层的克隆,克隆对象的所有实例变量。如数组,比数组的copy方法块近2倍
     public Person clone() throws CloneNotSupportedException{
        return (Person) super.clone();
    }

一、equals()与getclass()
equals方法,一般要重写
getClass(),返回对象的引用类型

public static void main(String[] args){
        //equals与getclass()方法
        Person p1 = new Person(15,160.9);
        Person p2 = new Person(15,160.9);
        Person p3 = new Person(10,150);

        System.out.println(p1.equals(p2));//true
        System.out.println(p1.equals(p3));//false
}

二、finalize()方法
垃圾回收机制回收某个对象的时候。会调用finalize()方法,清理对象所占资源。
此外,有可能使某个对象激活,达到可达状态。不会被激活的,才会被清理。

public static void main(String[] args){

        new Person(10,150);
        //finalize()方法
        System.gc();
        System.runFinalization();//调用finalize方法,ft引用到恢复到可达状态的变量
        p.info();//我的年龄是:10,我的身高是:150.0
    }

三、toString()方法

public static void main(String[] args){

        Person p = new Person(10,150);
        System.out.println(p.toString());
    }
不能确定一个引用变量是否为空时,调用对象的toString()方法会引发空指向异常
调用Object.toString(Object obj)不会引发空指向异常
this.name = Objects.requireNonNull(name);//为空指引检验

public class Object_class {
    private static Object_class o;
    public static void main(String[] args){

        //没有报空指引错误
        System.out.println(Objects.hashCode(o));//0
        System.out.println(Objects.toString(o));//null
    }
}

四、clone()方法

public static void main(String[] args) throws CloneNotSupportedException{

        Person p = new Person(10,150);
        Person p_clone = p.clone();//创建副本
        System.out.println(p==p_clone);//为false,不共享
        //年龄为:10,身高为:150.0
    }

//比数组的copy方法快
public static void main(String[] args) {

        int[] a = {1,2,3};
        int[] b = a.clone();
        for(int i:b){
            System.out.println(i);
        }
    }

五、hashCode()与identityHashCode()

public static void main(String[] args){

        String s1 = new String("hello");
        String s2 = new String("hello");

        //String类重写了hashCode(),改为根据字符序列计算hashcode
        //identityHashCode()根据地址计算hashcode
        System.out.println(s1.hashCode()+"----------"+s2.hashCode());
        System.out.println(System.identityHashCode(s1)
                           +"----------"+System.identityHashCode(s2));

        String s3 = "java";
        String s4 = "java";
        System.out.println(System.identityHashCode(s3)
                           +"----------"+System.identityHashCode(s4));


输出:
99162322----------99162322//hashcode认为s1与s2相等
366712642----------1829164700//根据地址计算的,认为s1与s2不相等
2018699554----------2018699554//s3与s4为常量池中,因此指向同一对象,地址相同
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值