第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用枚舉為對象分配標志

如何使用枚舉為對象分配標志

慕勒3428872 2022-01-06 17:48:13
我正在用 Java 創(chuàng)建一個棋盤游戲,并且我正在嘗試編寫一個方法來標記用戶在游戲過程中選擇的對象(對象代表棋盤上的一個圖塊)。該方法位于設置單個 Tile 在板上的值和位置的類中。我認為使用枚舉類型是個好主意,但我不確定如何實現(xiàn)這一點。在我的課程中,我有一些方法可以在網(wǎng)格上獲取 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 方法返回一個語句,那么它可以用來表明 tile 已經(jīng)被選中選擇了?;蛘摺以撛趺崔k。這是我到目前為止:public enum Status {CHOSEN, NOTCHOSEN};public static void tileStatus(Status stat){  switch(stat) {    case CHOSEN: //something       break;    case NOTCHOSEN: //something       break;  }}
查看完整描述

3 回答

?
有只小跳蛙

TA貢獻1824條經(jīng)驗 獲得超8個贊

您可以聲明 enum 是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();

  }


查看完整回答
反對 回復 2022-01-06
?
紫衣仙女

TA貢獻1839條經(jīng)驗 獲得超15個贊

向 Tile 添加布爾值可能會幫助您處理狀態(tài)。由于只有兩種可能的狀態(tài)(已選擇,未選擇),因此布爾值可能更有意義。也不要默認添加 getter 和 setter。只有在你需要它們的時候。參考“講不問原則”


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 類的內(nèi)部狀態(tài),因為它的獨立存在可能沒有意義。


查看完整回答
反對 回復 2022-01-06
?
翻過高山走不出你

TA貢獻1875條經(jīng)驗 獲得超3個贊

使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());


    }


}


查看完整回答
反對 回復 2022-01-06
  • 3 回答
  • 0 關(guān)注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號