以上过程为实现equals的标准过程

以下为定义equal(加上这个定义,返回ture或false)

public boolean equals(Object o){

 student s=(student)o;

 if (s.name.equals(this.name)&&s.age==this.age)

else return false;

}如果equals()返回的值为

 

以下为实现标准equals的流程:

public boolean equals(Object o){

 if (this==o) return trun; //此时两者相同

 if (o==null) return false;

 if (! o instanceof strudent) return false; //不同类

 studeng s=(student)o; //强制转换

  if (s.name.equals(this.name)&&s.age==this.age) return true;

else return false;

}

以上过程为实现equals的标准过程。

 1 package TomText;
 2 
 3 public class TomText_38 {
 4     private int day;
 5     private int month;
 6     private int year;
 7     public void setDate(int day,int month,int year){        
 8         this.day=day;
 9         this.month=month;
10         this.year=year;
11     }
12     public static void main(String[] args){
13         TomText_38 t=new TomText_38();
14         t.setDate(3, 7, 2018);
15         System.out.println(t.day);
16         System.out.println(t.month);
17         System.out.println(t.year);
18     }
19 
20 }
原文地址:https://www.cnblogs.com/borter/p/9419337.html