Object类
java.lang.Object
类 Object 是类层次结构的根(父)类。
每个类都使用 Object 作为超(父)类。
所有对象(包括数组)都实现这个类的方法。
Object类的toString() 方法
直接打印对象的地址值没有意义,需要重写Object类中的toString方法
打印对象的属性(name, age)
alt + inter 可自动导入
Person类:
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 36 37 38
| package com.tipdm.Demo01;
import java.util.Objects;
public class Person { private String name; private int age;
@Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; }
public Person(String name, int age) { this.name = name; this.age = age; }
public Person() { }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
|
demo1类:
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 36 37 38 39 40
| package com.tipdm.Demo01;
import java.util.ArrayList; import java.util.Random; import java.util.Scanner;
public class demo1 { public static void main(String[] args) {
Person p = new Person("张三", 18); String s = p.toString(); System.out.println(s);
System.out.println(p);
Random r = new Random(); System.out.println(r);
Scanner sc = new Scanner(System.in); System.out.println(sc);
ArrayList<Integer> list = new ArrayList<>(); list.add(1); list.add(2); list.add(3); System.out.println(list); } }
|
1 2 3 4 5 6 7
| com.tipdm.Demo01.Person@2b8768b5 com.tipdm.Demo01.Person@2b8768b5 java.util.Random@4554617c java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\x{2c}][decimal separator=\x{2e}][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q∞\E] [1, 2, 3]
进程已结束,退出代码0
|
Object类的equals()方法
Person类默认继承了Object类,所以可以使用Object类的equals方法
equals(Object obj) 指示其他某个对象是否与此对象“相等”。
alt + instert 可直接添加
equals 方法源码:
1 2 3
| public boolean equals(Object obj) { return (this == obj); }
|
参数:
== 比较运算符,返回的是一个boolean
基本数据类型:比较的是值
引用数据类型:比较的是内存地址
Person类:
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| package com.tipdm.Demo01;
import java.util.Objects;
public class Person { private String name; private int age;
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return age == person.age && Objects.equals(name, person.name); }
@Override public int hashCode() { return Objects.hash(name, age); }
public Person(String name, int age) { this.name = name; this.age = age; }
public Person() { }
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
|
demo02类:
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
| package com.tipdm.Demo01;
import java.util.ArrayList;
public class demo2 { public static void main(String[] args) {
Person p1 = new Person("迪丽热巴", 18); Person p2 = new Person("古力娜扎", 19); Person p3 = new Person("古力娜扎", 19); System.out.println("p1:" + p1); System.out.println("p2:" + p2);
boolean b = p2.equals(p3); System.out.println(b);
ArrayList<String> list = new ArrayList<>(); System.out.println(p1.equals(null)); } }
|
1 2 3 4 5 6
| p1:com.tipdm.Demo01.Person@904fec5d p2:com.tipdm.Demo01.Person@89b8c9fc true false
进程已结束,退出代码0
|
Objects.equals(s1, s2) 防止空指针异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.tipdm.Demo01;
import java.util.Objects;
public class demo3Objects { public static void main(String[] args) {
String s1 = null; String s2 = "aBc";
boolean b2 = Objects.equals(s1, s2); System.out.println(b2); } }
|