class Line
{
private Point start, end;
static interface Direction //内部接口总是静态的
{
int LEFT = 0;
int RIGHT = 1;
int UP = 2;
int DOWN = 3;
}
static class Point implements Direction //为什么这里也要静态?
{
int x, y;
Point()
{
this(0, 0);
}
Point(int x, int y)
{
this.x = x;
this.y = y;
}
Point(Point p)
{
this(p.x, p.y);
}
public String toString()
{
return "(" + this.x + ", " + this.y + ")";
}
private Point another(int length, int direction)
{
Point p = new Point(this);
switch(direction)
{
case LEFT: p.x -= length; break; //Point类如果没有声明实现Direction接口,它必须通过Direction.LEFT才能引用常量
case RIGHT: p.x += length; break;
case UP: p.y += length; break;
case DOWN: p.y -= length; break;
default: p = null;
}
return p;
}
}
Line(int x, int y, int length, int direction)
{
this.start = new Point(x, y);
this.end = start.another(length, direction);
}
public String toString()
{
return "一条直线,起点为" + this.start.toString() + ", 终点为" + this.end.toString();
}
}
class MyLineDemo
{
public static void main(String[] args)
{
Line l = new Line(100, 100, 20, Line.Direction.LEFT);
System.out.println(l);
}
}