我正在用Java创建一个棋盘游戏,并且试图编写一种方法来标记用户在游戏期间选择的对象(该对象代表棋盘上的图块)。该方法位于一个类中,该类设置单个Tile的值和在板上的位置。
我认为使用枚举类型是一个好主意,但是我不确定究竟如何实现它。在我的课程中,我有一些方法可以获取Tile在网格上的位置(行,列)及其代表的字母。
public class Tile {
private final String letter; //holds the letter value of the tile
private final int row; //holds tile row index
private final int column;
public Tile(String l, int r, int c) {
this.letter = l;
this.row = r;
this.column = c;
}
//setter&getter methods
public String toString() {
return this.getLetter()+" "+ this.getRow() +
"," + this.getColumn();
}
所以在这个类中,我也想编写一个方法来标记是否选择了tile对象...我在想,如果toString方法返回一条语句,那么可以用来表明选择了图块。或者...我应该怎么做。这是我到目前为止的内容:
public enum Status {CHOSEN, NOTCHOSEN};
public static void tileStatus(Status stat){
switch(stat) {
case CHOSEN: //something
break;
case NOTCHOSEN: //something
break;
}
}
答案 0 :(得分:1)
您可以声明枚举是Tile
类的实例成员
public class Tile {
private final String letter; //holds the letter value of the tile
private final int row; //holds tile row index
private final int column;
private Status flag; // use getter and setter to set flag on using Status enum
public Tile(String l, int r, int c) {
this.letter = l;
this.row = r;
this.column = c;
}
//setter&getter methods
public String toString() {
return this.getLetter()+" "+ this.getRow() +
"," + this.getColumn();
}
答案 1 :(得分:1)
向图块添加布尔值可以帮助您获得状态。由于只有两种可能的状态(选择的而不是未选择的),因此布尔值可能更有意义。也不要默认添加getter和setter。仅在需要它们时。请参阅"tell don't ask principle"
public class Tile {
private final String letter; //holds the letter value of the tile
private final int row; //holds tile row index
private final int column;
private boolean isTileFlagged;
public Tile(String l, int r, int c) {
this.letter = l;
this.row = r;
this.column = c;
isTileFlagged = false; // May be false to being with
}
// add getters/setters only when necessary
public void toggleFlaggedState(){
isTileFlagged = !isTileFlagged;
}
public String toString() {
return this.getLetter()+" "+ this.getRow() +
"," + this.getColumn();
}
// add hashcode, equals if necessary
此外,如果枚举是必需的,则它可能是Tile类的内部状态,因为它的独立存在可能没有意义。
答案 2 :(得分:0)
将enum
作为class
的成员变量和enum
的方法。
像下面这样:-
package com.robo.lab;
public class Tile {
private final String letter; // holds the letter value of the tile
private final int row; // holds tile row index
private final int column;
private Status status;
public Tile(String l, int r, int c,Status status) {
this.letter = l;
this.row = r;
this.column = c;
this.status=status;
}
// setter&getter methods
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public String toString() {
return this.getLetter() + " " + this.getRow() + "," + this.getColumn()+","+this.getStatus();
}
public String getLetter() {
return letter;
}
public int getRow() {
return row;
}
public int getColumn() {
return column;
}
}
package com.robo.lab;
public enum Status {
CHOSEN, NOTCHOSEN;
public static void tileStatus(Status stat) {
switch (stat) {
case CHOSEN: // something
break;
case NOTCHOSEN: // something
break;
}
}
}
package com.robo.lab;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Tile obj1= new Tile("AUser", 1, 1,Status.CHOSEN);
System.out.println(obj1.toString());
Tile obj2= new Tile("BUser", 1, 1,Status.NOTCHOSEN);
System.out.println(obj2.toString());
}
}