价值无法解决或不是一个领域

时间:2015-07-14 00:11:36

标签: java

如果public boolean mergesWith(Tile moving)true图块具有相同的值,则this方法会返回moving。但是,当我通过执行以下操作检查它们是否相同时:

if(this.value == temp.value){
    return true;
}

然后它显示temp.value上的错误,指出值无法解析或不是字段

我该如何解决?

Class TwoNTile

package Game2048;

// Concrete implementation of a Tile. TwoNTiles merge with each other
// but only if they have the same value.

public class TwoNTile extends Tile {

    private int value;

    // Create a tile with the given value of n; should be a power of 2
    // though no error checking is done
    public TwoNTile(int n){
        value = n;
    }

    // Returns true if this tile merges with the given tile. "this"
    // (calling tile) is assumed to be the stationary tile while moving
    // is presumed to be the moving tile. TwoNTiles only merge with
    // other TwoNTiles with the same internal value.
    public boolean mergesWith(Tile moving){
        Tile temp = moving;
        if(this.value == temp.value){
            return true;
        }
        else{
            return false;
        }
    }

    // Produce a new tile which is the result of merging this tile with
    // the other. For TwoNTiles, the new Tile will be another TwoNTile
    // and will have the sum of the two merged tiles for its value.
    // Throw a runtime exception with a useful error message if this
    // tile and other cannot be merged.
    public Tile merge(Tile moving){

        return null;
    }

    // Get the score for this tile. The score for TwoNTiles are its face
    // value.
    public int getScore(){

        return -1;
    }

    // Return a string representation of the tile
    public String toString(){

        return "";
    }
}

Class Tile

package Game2048;

// Abstract notion of a game tile.
public abstract class Tile{
  // Returns true if this tile merges with the given tile. 
  public abstract boolean mergesWith(Tile other);

  // Produce a new tile which is the result of merging this tile with
  // the other. May throw an exception if merging is illegal
  public abstract Tile merge(Tile other);

  // Get the score for this tile.
  public abstract int getScore();

  // Return a string representation of the tile
  public abstract String toString();

}

3 个答案:

答案 0 :(得分:3)

首先:您可以这样做:

public boolean mergesWith(Tile moving){
    return this.value == temp.value;
}

获得更优雅的解决方案。

第二:您需要将value变量添加到Tile类。

public abstract class Tile{
   // ...
   // add a value to Tile
   protected int value; 
   // ...
}

您已展开Tile并添加了新字段。该字段不是Tile字段,Tile不包含(看不到)字段。

根据以下评论

Tile中声明值时,您无需再次在TwoNTile中声明它。 您可以制作平铺对象,而不是使用构造函数。

Tile t = new TwoNTile(...);

是有效的Tile对象。这样,您就可以实现已经尝试过的逻辑。

在SO上查看Java中的static and dynamic binding,或google it

答案 1 :(得分:2)

您正尝试从没有该属性的抽象类访问该属性。

你需要将其削减到更高的水平,例如

TwoNTile temp = (TwoNTile) moving;

我意识到这个抽象类可能有多个扩展,但你需要将它切换到类层次结构中的最高级别,这样才能使用类或者它的后代来回答这个问题。

更新了方法:

public boolean mergesWith(Tile moving){
    TwoNTile temp = (TwoNTile) moving;
    if(this.value == temp.value){
        return true;
    }
    else{
        return false;
    }
}

答案 2 :(得分:1)

value课程中,您需要添加类变量package Game2048; // Abstract notion of a game tile. public abstract class Tile{ //need to add instance variable int value; // Returns true if this tile merges with the given tile. public abstract boolean mergesWith(Tile other); // Produce a new tile which is the result of merging this tile with // the other. May throw an exception if merging is illegal public abstract Tile merge(Tile other); // Get the score for this tile. public abstract int getScore(); // Return a string representation of the tile public abstract String toString(); } ,如下所示:

   for (i = 0; i < n; i++) {
       A[i] = value;
   }