彩笔笔记2016/12/5——改写equals

来源:互联网 发布:华通云数据与马云关系 编辑:程序博客网 时间:2024/06/10 03:21
package Object;
public class Graph{
 int edge;
 int area;
 int girth;
 public int getEdge() {
  return edge;
 }
 public void setEdge(int edge) {
  this.edge = edge;
 }
 public int getArea() {
  return area;
 }
 public void setArea(int area) {
  this.area = area;
 }
 public int getGirth() {
  return girth;
 }
 public void setGirth(int girth) {
  this.girth = girth;
 }
 
 
 @Override
 public boolean equals(Object obj) {                      //改写equals
  // TODO Auto-generated method stub
  if(this==obj){
   return true;
  }if(obj instanceof Graph){
   if(this.edge==edge){
    return true;
   }else if(this.girth==girth){
    return true;
   }else if(this.area==area){
    return true;
   }
  }
  return false;
 }
 public static void main(String[]args){
  Graph g1=new Graph();
  g1.setEdge(4);
  g1.setGirth(16);
  g1.setArea(16);
  Graph g2 = new Graph();
  g2.setEdge(3);
  g2.setGirth(16);
  g2.setArea(16);
  System.out.println(g1.equals(g2));
 }
}

0 0
原创粉丝点击