【问题标题】:Cannot instantiate the type Chessboard.Type [duplicate]无法实例化类型 Chessboard.Type [重复]
【发布时间】:2016-05-05 01:36:40
【问题描述】:

好的,我正在从事一个将 C++ 转换为 Java 的学校项目,我已经取得了相当大的进展。该项目用于国际象棋游戏。代码如下:

package Chess;

import java.util.Enumeration;

public class ChessBoard{
/* This enum represents values for each of the possible "pieces" that can
 * occupy a space on the chess board, including "blank" for a vacant space.
 */


public enum Type
{
  BLANK,
  PAWN,
  BISHOP,
  KNIGHT,
  ROOK,
  QUEEN,
  KING;

    public int getValue()
    {
        return this.ordinal();
    }

    public static Type forValue(int value)
    {
        return values()[value];
    }
}

public class Coord
{
  public short m_X;
  public short m_Y;
}

public class GlobalMembers
{
    /* These const values are used for identifying whether the "piece" occupying
     * a chess board space is black, white, or in the case of a blank spot,
     * "clear."
     */

    public static final short BLACK = 0;

    public static final short WHITE = 1;

    public static final short CLEAR = 2;

    public static final short CHECK_MOVE = 1; // this is a piece move "mode" designating an AI board evaluation

    public static final short REAL_MOVE = 2; // this is a piece move "mode" designating an actual AI move
}


public class ChessPiece
{

  /* The ctor for ChessPiece takes a piece type and color.
   */

  public ChessPiece(Type type, short color)
  {
      this.m_type = type;
      this.m_color = color;
      this.m_moves = 0;
  }
  public void dispose()
  {
  }

  /* This member function returns the chess piece type.
   */

  public final Type getType()
  {
    return m_type;
  }

  /* This member function returns the chess piece color.
   */

  public final short getColor()
  {
    return m_color;
  }

  /* This function is used to record a new movement of a piece. Movement tracking
   * is necessary for evaluating validity of special piece moves like en passant
   * capture and castling.
   */

  public final void incrementMoves()
  {
    ++m_moves;
  }

  /* Reduces the number of times a piece has been moved. Used for adjustment when
   * a function incidentally increments the movement of a piece in a case where
   * a board evaluation is taking place, and is not intended as an actual move.
   */

  public final void decrementMoves()
  {
    --m_moves;
  }

  /* Returns the number of times a piece has been moved.
   */

  public final int getMoves()
  {
    return m_moves;
  }
  private Type m_type = new Type(); // this variable holds the type of piece this object represents

  private short m_color; // this variable holds the color of the piece, white or black
  private int m_moves; // this variable holds the number of moves the piece has made
}

}

问题是当我开始的时候

private Type m_type = new Type();

它说明“无法实例化类型 Chessboard.Type”

现在我到处找,包括here

正如您所看到的,该程序与这个程序非常不同,并且任何时候您在程序中引用 Type 都会被抛出。有谁知道如何解决这一问题?

【问题讨论】:

  • 谢谢,我之前读过,但是我们不知道 m_type 类型是什么。这就是为什么我们不能调用一个特定的。同样在下面我已经说明了当您尝试指示特定类型时会发生什么。

标签: java enums


【解决方案1】:

停止尝试实例化枚举类型。

例如,你可以把初始化写成这样:

private Type m_type = Type.BLANK;

【讨论】:

  • 这并不能解决任何问题,它将拒绝更改为 insert () 以完成表达式,当您这样做时,它再次拒绝说更改为 Type Chess() 然后将其切换回 Type();
  • @DestryAmiott 你什么意思?此更改使代码为compile,我在您的代码中看不到insert ()
  • 哦抱歉,我还有新类型;在那里,谢谢你的快速回答!抱歉之前的帖子
【解决方案2】:

您不能实例化 Enum 类型的变量。

您应该将 UNKNOWN 添加到 Type,然后使用 Type.UNKNOWN 的默认值初始化 m_type,然后在构造函数中将其设置为适当的值。

【讨论】:

  • 代码中有一条注释说 BLANK 用于空置空间。使用 UNKNOWN,您知道那里应该有一块,但它没有被分配正确的类型。
  • 你说得对 - 很好!
【解决方案3】:

Type 是一个枚举,而不是可用于实例化对象的类。阅读有关枚举的更多信息here

【讨论】:

    猜你喜欢
    • 2011-12-19
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2019-07-26
    相关资源
    最近更新 更多