GS520

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  12 随笔 :: 0 文章 :: 0 评论 :: 1454 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

java中不管是任何对象都会继承Object类型,不管显示继承或隐式继承,都可以使用Object中的方法

  1. toString()

        该方法返回该实例对象的详细信息,必要时需要覆写,如果不覆写使用Object默认实现,该实现返回该对象的内存信息(简单java类一定会覆写)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Dog{
   
    private String sex;
    private int age;
     
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
     
    public String toString() {
        return "demo5.Dog:[name:"+name+",sex:"+sex+",age:"+age+"]";
    }
}

  

在子类中实现Object的toString()方法

public String toString() {

return this.getClass().getName()+"@"+Integer.toHexString(this.hashCode());

}

 

equals方法

指示其他某个对象是否与此对象“相等”。一般根据业务情况来进行覆写该方法,比如Dog类,产生不同对象,判断两个对象的属性是否一致,这种判断叫做”逻辑相等”,实现这种”逻辑相等”的业务,就需要覆写equals方法进行相等比较

覆写equals方法

覆写原则

  1. 自反性:对于任何非空引用值 x,x.equals(x) 都应返回 true。(自己和自己对比,返回true)
  2. 对称性:对于任何非空引用值 x 和 y,当且仅当 y.equals(x) 返回 true 时,x.equals(y) 才应返回 true。
  3. 传递性:对于任何非空引用值 x、y 和 z,如果 x.equals(y) 返回 true,并且 y.equals(z) 返回 true,那么 x.equals(z) 应返回 true。(好比如A大于B,B大于C,A一定大于C)
  4. 一致性:对于任何非空引用值 x 和 y,多次调用 x.equals(y) 始终返回 true 或始终返回 false,前提是对象上 equals 比较中所用的信息没有被修改。
  5. 对于任何非空引用值 x,x.equals(null) 都应返回 false

  6. 代码示例

     

    public class Dog{

    private String name;

    private String sex;

    private int age;

     

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public String getSex() {

    return sex;

    }

    public void setSex(String sex) {

    this.sex = sex;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

     

    /*

    public String toString() {

    return "demo5.Dog:[name:"+name+",sex:"+sex+",age:"+age+"]";

    }*/

     

    public String toString() {

    return this.getClass().getName()+"@"+Integer.toHexString(this.hashCode());

    }

     

    public boolean equals(Object obj) {

    //1.null值比对,一定返回false

    if(obj == null) {

    return false;

    }

    //2.非该类型一定返回false

    // instanceof 关键字用于判断对象的类型是否指定类型   左边是要对比的对象,右边是指定的类型

    if(!(obj instanceof Dog)) {

    return false;

    }

     

    //3.对比两条狗属性是否相同

    Dog dog = (Dog)obj;

    /**

    if(this.name.equals(dog.getName())

    && this.age == dog.getAge()

    && this.getSex().equals(dog.getSex())) {

    return true;

    }

    return false;

    **/

    return this.name.equals(dog.getName())

    &&this.age == dog.getAge()

    && this.getSex().equals(dog.getSex());

    }

     

    }

     

    覆写equals方法必须覆写hashCode方法

    如果两个调用equals相等,那么hashCode值也相等, 两个不相等的对象调用hashCode方法返回的值,一定不等

    代码示例

    public class Dog{

    private String name;

    private String sex;

    //int类型数据类型

    private Integer age;

     

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public String getSex() {

    return sex;

    }

    public void setSex(String sex) {

    this.sex = sex;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

     

    /*

    public String toString() {

    return "demo5.Dog:[name:"+name+",sex:"+sex+",age:"+age+"]";

    }*/

     

    public String toString() {

    return this.getClass().getName()+"@"+Integer.toHexString(this.hashCode());

    }

     

    public boolean equals(Object obj) {

    //1.null值比对,一定返回false

    if(obj == null) {

    return false;

    }

    //2.非该类型一定返回false

    // instanceof 关键字用于判断对象的类型是否指定类型   左边是要对比的对象,右边是指定的类型

    if(!(obj instanceof Dog)) {

    return false;

    }

     

    //3.对比两条狗属性是否相同

    Dog dog = (Dog)obj;

    /**

    if(this.name.equals(dog.getName())

    && this.age == dog.getAge()

    && this.getSex().equals(dog.getSex())) {

    return true;

    }

    return false;

    **/

    return this.name.equals(dog.getName())

    &&this.age == dog.getAge()

    && this.getSex().equals(dog.getSex());

    }

     

    /**

     * hashCode简单实现

     */

    public int hashCode() {

    int result = 20;

    result = result * 31 + this.name.hashCode();

    result = result * 31 + this.age.hashCode();

    result = result * 31 + this.sex.hashCode();

    return result;

    }

    }

    了解:散列值算法MD5/MD4/SHA-256

    运行代码

     

    public class App {

    public static void main(String[] args) {

    Dog g = new Dog();

    g.setName("大黄");

    g.setSex("公");

    g.setAge(12);

     

    Dog g1 = new Dog();

    g1.setName("大黄");

    g1.setSex("公");

    g1.setAge(11);

     

    System.out.println(g.equals(g));

     

    System.out.println(g.hashCode());

    System.out.println(g1.hashCode());

    }

    }

     

    运行结果

    true

    719595913

    719595882

     

     

    Object类型的使用场景

    Object类型一般用于接受子类对象,也就是作通用变量类型使用

     

    abstract class Animal{

    /**

     * 用于对比两个动物的地址值(示例方法,只做参考)

     * @param obj

     * @return

     */

    public boolean eq(Object object) {

    return this == object;

    }

    }

    class Cat extends Animal{

    }

     

    class Dog extends Animal{

    }

     

     

     

     

    public class Demo {

    public static void main(String[] args) {

    new Cat().eq(new Dog());

    }

    }

    Animal中编写eq方法用于给子类提供对比方法,但是不知道具体有哪些子类需要对比,所以使用Object类型作为参数类型,这样就可以让所有子类都可以使用该方法进行对比

    (Object不能限定类型范围,所以一般在需要限定范围的地方不会使用Object,万一使用需要校验传进来对象是否符合需求)

posted on   CL396  阅读(5)  评论(0编辑  收藏  举报
编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
· 全程使用 AI 从 0 到 1 写了个小工具
点击右上角即可分享
微信分享提示