码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-08-04 18:52:12      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:style   code   ==   public   vat   stat   als   static   col   

以下为定义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 }

 

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

标签:style   code   ==   public   vat   stat   als   static   col   

原文地址:https://www.cnblogs.com/borter/p/9419337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!