1:代码:
1 package com.lieyan; 2 3 public class TestStudent { 4 public static void main(String[] args) { 5 6 // getClass方法 7 Student s1 = new Student("aaa", 20); 8 Student s2 = new Student("bbb", 22); 9 // 输出类型 同为Student类型 10 System.out.println(s1.getClass()); 11 System.out.println(s2.getClass()); 12 System.out.println("================================"); 13 14 //hasCode方法 15 System.out.println(s1.hashCode()); 16 System.out.println(s2.hashCode()); 17 Student s3 = s1; 18 System.out.println(s3.hashCode()); 19 System.out.println("================================"); 20 21 // toString方法 22 System.out.println(s1); 23 System.out.println(s2.toString()); 24 System.out.println("================================"); 25 26 // euqals方法,判断两个对象是否相等 27 System.out.println(s1.equals(s2)); 28 System.out.println(s1.equals(s3)); 29 30 Student s4 = new Student("xiaoa", 1); 31 Student s5 = new Student("xiaoa", 1); 32 System.out.println(s4.equals(s5)); 33 } 34 }
2:结果
1 class com.lieyan.Student 2 class com.lieyan.Student 3 ================================ 4 1846274136 5 1639705018 6 1846274136 7 ================================ 8 Student{name='aaa', age=20} 9 Student{name='bbb', age=22} 10 ================================ 11 false 12 true 13 false