01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package types.point4;
import java.awt.Color;
interface Colored {
Color getColor();
}
interface Point {
double getX();
double getY();
}
final class CartesianPoint implements Colored, Point {
private final double x;
private final double y;
private final Color color;
public CartesianPoint(double x, double y, Color color) {
this.x = x;
this.y = y;
this.color = color;
}
public double getX() { return x; }
public double getY() { return y; }
public Color getColor() {
System.out.println("It's Cartesian!");
return color;
}
}
final class PolarPoint implements Colored, Point {
private final double theta;
private final double r;
private final Color color;
public PolarPoint(double theta, double r, Color color) {
this.theta = theta;
this.r = r;
this.color = color;
}
public double getX() { return r*Math.cos(theta); }
public double getY() { return r*Math.sin(theta); }
public Color getColor() {
System.out.println("It's Polar!");
return color;
}
}
public class Main {
private Main() {}
public static void main(String[] args) {
CartesianPoint p1 = new CartesianPoint(0,0,Color.RED);
CartesianPoint q1 = new CartesianPoint(1,1,Color.BLUE);
PolarPoint r1 = new PolarPoint(0,0,Color.RED);
Point p2 = p1;
Point q2 = q1;
Point r2 = r1;
Colored p3 = p1;
Colored q3 = q1;
Colored r3 = r1;
}
}
|