Object类中的equals方法
package com.Object;
import javax.swing.*;
import java.util.Objects;
public class Demo01 {
public static void main(String[] args) {
Student s1 = new Student("Li",18);
Student s2 = new Student("Li",18);
/**
* equals与"=="对比:
* "=="是用来判断地址是否完全一致
* equals是用来判断值是否完全一致(可以在类中进行重写)
*/
if(s1.equals(s2))
System.out.println("Yes");
else
System.out.println("No");
if(s1 == s2)
System.out.println("Yes");
else
System.out.println("No");
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
class Student{
String name;
int age;
public Student(String name,int age){
this.name = name;
this.age = age;
}
/**
* 对Object类中的equal方法重写,判断两个对象是否完全一致
* (成员变量完全相等)
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age &&
Objects.equals(name, student.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
Object类中的toString()方法
package com.Object;
public class Demo02 {
public static void main(String[] args) {
Person p1 = new Person("小明",19);
Person p2 = new Person("小红",22);
System.out.println(p1.toString());
System.out.println(p2.toString());
}
}
class Person{
String name;
int age;
public Person(String name,int age){
this.age = age;
this.name = name;
}
/**
* 对Object类中的toString方法进行重写
* 返回成员变量的值
*/
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
I'm so cute. Please give me money.
- 本文链接:http://yoursite.com/2020/04/25/Object%E7%B1%BB/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。
若没有本文 Issue,您可以使用 Comment 模版新建。
GitHub IssuesGitHub Discussions