import java.awt.*;class Point { private int x = 0; private int y = 0; public Point() { } public Point(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (!(o instanceof Point))return false; Point p = (Point)o; return p.x == x && p.y == y; } }class ColorPoint extends Point { private Point point; private Color color; public ColorPoint(int x, int y, Color color) { point = new Point(x, y); this.color = color; } public Point asPoint() { return point; } public boolean equals(Object o) { if (!(o instanceof ColorPoint))return false; ColorPoint cp = (ColorPoint)o; return cp.point.equals(point) && cp.color.equals(color); } }class PointTest{ public static void main(String[] str){ Point p=new Point(1,2); ColorPoint cp1=new ColorPoint(1,2,Color.RED); ColorPoint cp2=new ColorPoint(1,2,Color.BLUE); System.out.println(cp1.equals(p)); System.out.println(p.equals(cp1)); System.out.println(p.equals(cp2)); System.out.println(cp1.equals(cp2)); } }自己看看
可以编译啊,我在dos下面直接编译是可以的啊 只不过编译不过而已 呵呵!
class Point { private final int x; // private int x; private final int y; // private int y; 你试一下
public Point(int x, int y) { this.x = x; this.y = y; } public boolean equals(Object o) { if (!(o instanceof Point))return false; Point p = (Point)o; return p.x == x && p.y == y; } }
private int x = 0;
private int y = 0; public Point() { }
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (!(o instanceof Point))return false;
Point p = (Point)o;
return p.x == x && p.y == y;
}
}class ColorPoint extends Point {
private Point point;
private Color color;
public ColorPoint(int x, int y, Color color) {
point = new Point(x, y);
this.color = color;
}
public Point asPoint() {
return point;
}
public boolean equals(Object o) {
if (!(o instanceof ColorPoint))return false;
ColorPoint cp = (ColorPoint)o;
return cp.point.equals(point) && cp.color.equals(color);
}
}class PointTest{
public static void main(String[] str){
Point p=new Point(1,2);
ColorPoint cp1=new ColorPoint(1,2,Color.RED);
ColorPoint cp2=new ColorPoint(1,2,Color.BLUE);
System.out.println(cp1.equals(p));
System.out.println(p.equals(cp1));
System.out.println(p.equals(cp2));
System.out.println(cp1.equals(cp2));
}
}自己看看
只不过编译不过而已
呵呵!
private final int x; // private int x;
private final int y; // private int y; 你试一下
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object o) {
if (!(o instanceof Point))return false;
Point p = (Point)o;
return p.x == x && p.y == y;
}
}