Java面向对象-Object类

本文详细解析了Java中Object类的toString和equals方法,通过实例展示了如何重写这两个方法以实现更符合业务需求的对象表示和比较。理解并掌握这些方法对于Java开发至关重要。

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

Object类是所有类的父类;

 

Object类的常用方法

1,public String toString() 返回该对象的字符串表示。

2,public boolean equals(Object obj) 指示其他某个对象是否与此对象“相等”

我们上一个示例:

1

2

3

4

5

6

7

8

9

10

11

12

13

package com.java.chap03.sec14;

 

public class A{

 

    /**

     * Object是所有类的父类

     */

    public A() {

        super();

        // TODO Auto-generated constructor stub

    }

 

}

 

我们alt+shift+s 弹出菜单 ,然后Generate Constructors from Superclass 自动从父类生成构造方法,这里的super() 我们鼠标先移动上去

然后按住ctrl,然后点进去,会进入到Object类:

接下来讲toString()方法:

我们先上一个类:

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

package com.java.chap03.sec14;

 

public class People {

 

    private String name;

 

    public People(String name) {

        super();

        this.name = name;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

 

    public static void main(String[] args) {

        People p1=new People("张三");

        System.out.println(p1);

        System.out.println(p1.toString());

    }

}

 

定义了一个name属性 生成了get set方法 以及main方法测试 

 

运行输出:

com.java.chap03.sec14.People@15db9742

com.java.chap03.sec14.People@15db9742

 

输出对象,默认调用的是toString()方法;

 

这里我们通过修改toString()实现,来实现输出对象的结果:

1

2

3

4

5

@Override

public String toString() {

    // TODO Auto-generated method stub

    return super.toString();

}

 

这里调用的是Object默认实现,我们来修改下:

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

package com.java.chap03.sec14;

 

public class People {

 

    private String name;

 

    public People(String name) {

        super();

        this.name = name;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

     

     

    @Override

    public String toString() {

        return this.getName();

    }

 

    public static void main(String[] args) {

        People p1=new People("张三");

        System.out.println(p1);

        System.out.println(p1.toString());

    }

}

 

运行输出:

张三

张三

 

 

实际开发中,有时候也会用到;需要掌握;

 

最后说下equals 是比较对象的引用,是否指向同一个堆内存;

我们上下代码:

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

package com.java.chap03.sec14;

 

public class People {

 

    private String name;

 

    public People(String name) {

        super();

        this.name = name;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

     

 

    public static void main(String[] args) {

        People p1=new People("张三");

        People p2=new People("张三");

        System.out.println(p1.equals(p2));

    }

}

 

运行输出:

false

 

我们来重写下equals方法,来实现比较具体内容:

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

32

33

34

35

package com.java.chap03.sec14;

 

public class People {

 

    private String name;

 

    public People(String name) {

        super();

        this.name = name;

    }

 

    public String getName() {

        return name;

    }

 

    public void setName(String name) {

        this.name = name;

    }

     

     

 

    @Override

    public boolean equals(Object obj) {

        String name=((People)obj).getName();

        return this.name==name;

    }

 

    public static void main(String[] args) {

        People p1=new People("张三");

        People p2=new People("张三");

        People p3=new People("李四");

        System.out.println("p1.equals(p2):"+p1.equals(p2));

        System.out.println("p1.equals(p3):"+p1.equals(p3));

    }

}

运行输出:

p1.equals(p2):true

p1.equals(p3):false

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值