62,628
社区成员
发帖
与我相关
我的任务
分享package org.lyk.entities;
public class Point
{
private int x;
private int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj)
{
if(obj==null)
return false;
if(this == obj)
return true;
if(!(obj instanceof Point))
return false;
Point point = (Point)obj;
if(this.x == point.x && this.y == point.y)
return true;
else
return false;
}
}package org.lyk.entities;
public class ColoredPoint extends Point
{
private String color;
public ColoredPoint(int x, int y,String color)
{
super(x, y);
this.color = color;
// TODO Auto-generated constructor stub
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
ColoredPoint other = (ColoredPoint) obj;
if (color == null)
{
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
return true;
}
}
package org.lyk.main;
import java.util.HashSet;
import org.lyk.entities.*;
public class Main
{
public static void main(String[] args)
{
Point p = new Point(1, 2);
ColoredPoint cp1 = new ColoredPoint(1, 2, "RED");
ColoredPoint cp2 = new ColoredPoint(3, 2, "RED");
System.out.println(cp1.equals(cp2));
}
}