public boolean euqals(Object obj){
return this == obj;
}
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n– != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
public boolean equals(Object obj)
class Point{
final int x;
final int y;
public void Point(int x, int y)
this.x = x;
this.y = y;
}
public boolean euqals(Point obj){
return (this.x == obj.x && this.y == obj.y);
}
}
Point a(1, 2); Poinr b(1, 2); System.out.println(a.equals(b));// 输出true
import java.util.HashSet; HashSet<Point> coll = new HashSet<Point>(); coll.add(a); System.out.println(coll.contains(b));// 输出false
public class ColoredPoint extends Point {
private final Color color;
public ColoredPoint(int x, int y, Color color) {
super(x, y);
this.color = color;
}
@Override
public boolean equals(Object other) {
boolean result = false;
if (other instanceof ColoredPoint) {
ColoredPoint that = (ColoredPoint) other;
result = (this.color.equals(that.color) && super.equals(that));
}
return result;
}
}
Point p = new Point(1, 2); ColoredPoint cp = new ColoredPoint(1, 2, Color.RED); System.out.println(p.equals(cp)); //输出ture System.out.println(cp.equals(p)); //输出false
@overwrite
public boolean equals(Object obj){
if((obj instanceof Point)){
return false;
}
if(!(obj instanceof ColoredPoint)){
return obj.equals(this);
}
return super.equals(obj) && ((ColoredPoint)obj).color == color;
}
ColoredPoint cp1 = new ColoredPoint(1, 2, Color.RED); Point p = new Point(1, 2); ColoredPoint cp2 = new ColoredPoint(1, 2, Color.BLUE); System.out.println(cp1.equals(p)); //true System.out.println(p.equals(cp2)); //true System.out.println(cp1.equals(cp2)); //false
class ColoredPoint{
private final Point point;
private final Color color;
public Point asPoint(){
return point;
}
public boolean equals(Object obj){
boolean ret = false;
if(obj instanceof ColoredPoint){
ColoredPoint that = (ColoredPoint)obj;
ret = that.point.equals(point) && color.equals(that.color);
}
return ret;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有