java中String类中的这个boolean equals(Object obj),为什么括号里写Object,而不是String。

是因为重写了父类的方法吗

qq_慕婉清9181605
浏览 966回答 3
3回答

慕设计2395807

LRQAMCSLOFSXBXDJGMGZFXYBTDZCCLVSBNJPEEAHHTQANJYYXDDCSUEOAVYUNUKWQIATJIMJFVPQKTLKPEOLRKKAEAMSLYUGJAXAWZBNHEDLLYSOHAWCIFECPENKGPWSFVFXUJSBIOUUKNUXTPIODCWFLMAJSLIIEHWDNRDWZVVYDGTDWVUAYEDGJGZVBNJMSDJZJPZVRDZJZMNSYCYLOQAWTWTJDLLOQGNAPMJEGJSCSEQPFJKGJMNDZCVJMAIYUAXJJMMJPRQQDMJFUIIXWBOURUQTICLNKTVUAWZQRVXGMMEHNWJWVOXTJMJIEEAMIRYEAAZFMZHGFLVGSPSSDOLERUWGMCZTOXAODTIYBROUPXHJMOEGQNYUWFIYHQTJTACLOQGPLOKOCKTWYBKAWHDUQYBESHXAQMJRHJMBRHWTCTIRHPYBKNQGSGPSMCXNWGJBKACLUDTWEBWLAWIYOUUYZHWYUWTWMBEOIFADLOXLAKIVRGDYBRHWFXNWFUWGVYSIFWSODZJEUVYOXGVYNCMHDADKGCYNQZPKNKZBEGPLIDNWTSOQTBKZHXOMBXZPXSIXGSPZBXTVLVQGKADFUKGPRAVLIJLAPEAXZVZIPQAIYUXNPFWZOWTVRUJMQZWENCRUETPFWFOQTBYLBKVYPEGPEHQZBFBJZVESVRNXNDSBXZIFSOEIYVWZIKTJLUYBUCSOWTILOXUTIRHJMBRSCYSHDZMPSADHDKWSIYRNSVVCIWZOKAJSIETAPYOPFVENKUIEUJDMJYHEBUQFBDGVLGQTZBKTCRGPMCMNCYACMVLHIFDFHXEUDSIYPWZMJRUQGWAJRGPYUJRUENJFHJYOROKADMVXSVQNDSISBOQSPKADTWSI

逆光之羽

看了下源码 传入参数为anObject,里面一个判断是 this == anObject 应该是只有内存地址相关才会为true另个一个anObject instanceof String,直接指定了判断类型是否为String以上似乎只有传入参数类型为String才会进行判断 其他情况都是直接return false所以个人理解入参写成Object类型只是写通用化模块的编码习惯的问题 public boolean equals(Object anObject) {    if (this == anObject) {    //如果内存地址相同        return true;    }    if (anObject instanceof String) {        //如果是String类型实例        String anotherString = (String)anObject;        int n = count;        if (n == anotherString.count) {        char v1[] = value;        char v2[] = anotherString.value;        int i = offset;        int j = anotherString.offset;        while (n-- != 0) {            if (v1[i++] != v2[j++])            return false;        }        return true;        }    }    return false; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java