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 + ")";
}

}

原文地址:https://www.cnblogs.com/tuolemi/p/6360999.html