一起Talk Android吧(第八十三回:Java中的超级类-Object综合练习)

本文通过具体实例演示了Java中Object类的toString、equals及hashCode方法的应用,并对比了使用Arrays类提供的相应方法的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

各位看官们,大家好,上一回中咱们说的是Java中的超级类-Object之toString的例子,这一回咱们说的例子是:Object综合练习。闲话休提,言归正转。让我们一起Talk Android吧!

看官们,我们在前面章回中介绍了Object类和它的三大方法,这一回中主要是通过具体的例子对这三大方法方法进行综合练习,因此理论的内容少,大部分是实战代码:

public class ObjectEx {
    public static void main(String args[]) {
        //定义两个内容相同的数组
        int [] array1 = new int [] {1,2,3};
        int [] array2 = new int [] {1,2,3};

        //定义三个CustomClass类型的变量,其中第一个和第三个变量的内容相同
        CustomClass customValue1 = new CustomClass(1,"AAA");
        CustomClass customValue2 = new CustomClass(2,"BBB"); 
        CustomClass customValue3 = new CustomClass(1,"AAA");

        //使用没有重写的toString方法输出数组信息
        System.out.println("array1 = "+array1.toString());
        System.out.println("array2 = "+array2.toString());
        //使用没有Arrays类中重写的toString方法输出数组信息
        System.out.println("array1 = "+Arrays.toString(array1));
        System.out.println("array2 = "+Arrays.toString(array2));

        //使用没有重写的equals方法判断两个数组是否相同
        if(array1.equals(array2))
            System.out.println("array1 == array2");
        else
            System.out.println("array1 != array2");

        //使用Arrays类中重写的equals方法判断两个数组是否相同
        if(Arrays.equals(array1, array2))
            System.out.println("array1 == array2");
        else
            System.out.println("array1 != array2");

        //使用没有重写的hashCode方法输出数组的散列码
        System.out.println("array1 hascode = "+array1.hashCode());
        System.out.println("array2 hascode = "+array2.hashCode());
        //使用Arrays类中重写的hashCode方法输出数组的散列码
        System.out.println("array1 hascode = "+Arrays.hashCode(array1));
        System.out.println("array2 hascode = "+Arrays.hashCode(array2));

        //使用CustomClass类中重写的toString方法输出每个变量的信息
        System.out.println("customValue1 = "+customValue1);
        System.out.println("customValue2 = "+customValue2);
        System.out.println("customValue3 = "+customValue3);

        //使用CustomClass类中重写的hashCode方法输出每个变量的散列码
        System.out.println("customValue1 hascode = "+customValue1.hashCode());
        System.out.println("customValue2 hascode = "+customValue2.hashCode());
        System.out.println("customValue3 hascode = "+customValue3.hashCode());

        //使用CustomClass类中重写的equals方法判断 两个变量是否相同 
        if(customValue1.equals(customValue2))
            System.out.println("customValue1 == customValue2");
        else
            System.out.println("customValue1 != customValue2");

        if(customValue1.equals(customValue3))
            System.out.println("customValue1 == customValue3");
        else
            System.out.println("customValue1 != customValue3");
    }

    //自定义一个名叫CustomClass的类,类中只有两个成员变量, 类中重写了Object类的三个方法
    public static class CustomClass {

        private int mIntValue;
        private String mStrValue;

        public CustomClass(int intValue,String strValue) {
            this.mIntValue = intValue;
            this.mStrValue = strValue;
        }

        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if(obj != null && obj.getClass() == this.getClass()) {
                if(obj == this)
                    return true;

                CustomClass cusObj = (CustomClass)obj;
                if(cusObj.mIntValue == this.mIntValue && cusObj.mStrValue.equals(this.mStrValue))
                    return true;
            }

            return false;
        }

        @Override
        public int hashCode() {
            // TODO Auto-generated method stub
            return Objects.hash(mIntValue,mStrValue);
        }

        @Override
        public String toString() {
            // TODO Auto-generated method stub
            String className = this.getClass().getName();
            return className+"[mIntValue: "+mIntValue+" mStrValue: "+mStrValue+" ]";
        }
    }
}

看官们,我在代码中添加了详细的注释,大家可以通过注释来理解代码,也可以自己运行该代码来分析。下面是该代码的运行结果,请参考:

array1 = [I@1db9742
array2 = [I@106d69c
array1 = [1, 2, 3]
array2 = [1, 2, 3]
array1 != array2
array1 == array2
array1 hascode = 31168322
array2 hascode = 17225372
array1 hascode = 30817
array2 hascode = 30817
customValue1 = ObjectEx$CustomClass[mIntValue: 1 mStrValue: AAA ]
customValue2 = ObjectEx$CustomClass[mIntValue: 2 mStrValue: BBB ]
customValue3 = ObjectEx$CustomClass[mIntValue: 1 mStrValue: AAA ]
customValue1 hascode = 65537
customValue2 hascode = 66561
customValue3 hascode = 65537
customValue1 != customValue2
customValue1 == customValue3

各位看官,关于Java中的超级类-Object综合练习的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talk_8

真诚赞赏,手有余香

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值