JAVA大作业汇总2

JAVA大作业2

代码

package thegreatwork;

//Enum一般用来表示一组相同类型的常量,这里用于表示运动方向的枚举型常量,每个方向对象包括方向向量。
public enum Direction {
// 定义上下左右
UP(0, -1), DOWN(0, 1), LEFT(-1, 0), RIGHT(1, 0);

private final int y;
private final int x;
//final修饰的变量不能被继承

Direction(int x, int y) {
    this.x = x;
    this.y = y;
}

// 检索方向向量的X分量
public int getX() {
    return x;
}

// 检索方向向量的Y分量
public int getY() {
    return y;
}

@Override
//@Override这个句话下边的方法是继承父类的方法,对其覆盖
public String toString() {
    return name() + "(" + x + ", " + y + ")";
}

}

posted @ 2017-02-18 13:12  20145202马超  阅读(741)  评论(0编辑  收藏  举报