Object类
在Java中Object类是所有类的父类,也就是说Java的所有类都继承了Object。子类可以使用Object的所有方法
public class Person01 {
private String nickname;
private int age;
private int gender;
public Person01() {
}
public Person01(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
输出对象的引用时,实际上调用的是当前对象的toString方法
public class Person01Test {
@Test
public void test01(){
Person01 person01 = new Person01();
System.out.println(person01);
}
@Test
public void test02(){
Person01 person01 = new Person01();
System.out.println(person01.toString());
}
}
输出该类的完全限定名(包名+类名+地址符号)
com.lihaozhe.bean.Person01@1e4a7dd4
重写Object类的toString方法
public class Person02 {
private String nickname;
private int age;
private int gender;
public Person02() {
}
public Person02(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
@Override
public String toString() {
// return this.getClass().getSimpleName();初始写法
return "Person02(nickname="+this.nickname+",age="+this.age+",gender="+this.gender+")";
}
}
给实例化对象赋值
public class Person02Test {
@Test
public void test02(){
Person02 Person02 = new Person02("张三",18,1);
System.out.println(Person02.toString());
}
}
输出
Person02(nickname=张三,age=18,gender=1)
Object类的hashCode方法
源代码
/**
* Returns a string representation of the object.
* @apiNote
* In general, the
* {@code toString} method returns a string that
* "textually represents" this object. The result should
* be a concise but informative representation that is easy for a
* person to read.
* It is recommended that all subclasses override this method.
* The string output is not necessarily stable over time or across
* JVM invocations.
* @implSpec
* The {@code toString} method for class {@code Object}
* returns a string consisting of the name of the class of which the
* object is an instance, the at-sign character `{@code @}', and
* the unsigned hexadecimal representation of the hash code of the
* object. In other words, this method returns a string equal to the
* value of:
* <blockquote>
* <pre>
* getClass().getName() + '@' + Integer.toHexString(hashCode())
* </pre></blockquote>
*
* @return a string representation of the object.
*/
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
/**
* Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
* <p>
* The general contract of {@code hashCode} is:
* <ul>
* <li>Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code equals} comparisons on the object is modified.
* This integer need not remain consistent from one execution of an
* application to another execution of the same application.
* <li>If two objects are equal according to the {@link
* equals(Object) equals} method, then calling the {@code
* hashCode} method on each of the two objects must produce the
* same integer result.
* <li>It is <em>not</em> required that if two objects are unequal
* according to the {@link equals(Object) equals} method, then
* calling the {@code hashCode} method on each of the two objects
* must produce distinct integer results. However, the programmer
* should be aware that producing distinct integer results for
* unequal objects may improve the performance of hash tables.
* </ul>
*
* @implSpec
* As far as is reasonably practical, the {@code hashCode} method defined
* by class {@code Object} returns distinct integers for distinct objects.
*
* @return a hash code value for this object.
* @see java.lang.Object#equals(java.lang.Object)
* @see java.lang.System#identityHashCode
*/
@IntrinsicCandidate
public native int hashCode();
调取类的hashCode值
ublic class Person03Test {
@Test
public void test01(){
Person03 person03 = new Person03();
System.out.println(person03.hashCode());
System.out.println(Integer.toHexString(person03.hashCode()));
System.out.println(person03.toString());
}
}
hashCode值输出一个十进制数然后把十进制数转换成一个十六进制数
1330754528
4f51b3e0
com.lihaozhe.bean.Person03@4f51b3e0
重写Object类的equals方法
重写equals方法的时候一定要重写hashCode方法
重写比较Object类与Person04类的属性值
public class Person04 {
private String nickname;
private int age;
private int gender;
public Person04() {
}
public Person04(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
// 传进来的比较对象
Person04 person04 = (Person04) obj;
if(this.age!=person04.getAge()){
return false;
}else if (this.gender!=person04.getGender()){
return false;
}else if (!Objects.equals(this.nickname,person04.nickname)){
return false;
}else {
return true;
}
}
}
=等号比较的是内存地址也就是hashCode的值
equals比较的是属性内容
public class Person04Test {
@Test
public void test01(){
Person04 person01 = new Person04("张三",18,1);
Person04 person02 = new Person04("李四",28,0);
System.out.println(person01==person02);
System.out.println(person01.equals(person02));
}
@Test
public void test02(){
Person04 person01 = new Person04("张三",18,1);
Person04 person02 = new Person04("张三",18,1);
System.out.println(person01==person02);
System.out.println(person01.equals(person02));
}
}
输出
false
false
false
true
重写Object类的hashCode方法
传一个参考值reference=31;
public class Person05 {
private String nickname;
private int age;
private int gender;
public Person05() {
}
public Person05(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
@Override
public boolean equals(Object obj) {
// 传进来的比较对象
// 向下转型
Person05 person04 = (Person05) obj;
if(this.age!=person04.getAge()){
return false;
}else if (this.gender!=person04.getGender()){
return false;
}else if (!Objects.equals(this.nickname,person04.nickname)){
return false;
}else {
return true;
}
}
@Override
public int hashCode() {
// 设置一个参考值
int reference = 31;
return this.age+(this.gender+reference)+this.nickname.hashCode();
}
}
比较hashCode的值
public class Person05Test {
@Test
public void test01(){
Person05 person01 = new Person05("张三",18,1);
Person05 person02 = new Person05("李四",28,0);
Person05 person03 = new Person05("张三",18,1);
System.out.println(person01.hashCode());
System.out.println(person02.hashCode());
System.out.println(person03.hashCode());
}
}
输出hashCode的值同时也可以判断出是否是同一个对象
774939
842120
774939