Java 值无法解析或不是字段

Java 值无法解析或不是字段,java,Java,如果this和moving平铺具有相同的值,则方法public boolean mergesWith(平铺移动)返回true。但当我通过以下操作检查它们是否相同时: if(this.value == temp.value){ return true; } 然后它显示了温度值上的错误,表示值无法解析或不是字段 我怎样才能解决这个问题 类TwoNTile: package Game2048; // Concrete implementation of a Tile. TwoNTiles

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

if(this.value == temp.value){
    return true;
}
然后它显示了
温度值
上的错误,表示值无法解析或不是字段

我怎样才能解决这个问题

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

}
平铺

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

}

平铺
类中,需要添加类变量
,如下所示:

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

}

首先:您可以执行以下操作:

public boolean mergesWith(Tile moving){
    return this.value == temp.value;
}
以获得更优雅的解决方案

Second:您需要将
变量添加到
平铺
类中

public abstract class Tile{
   // ...
   // add a value to Tile
   protected int value; 
   // ...
}
您已经扩展了
平铺
并添加了新字段。该字段不是
平铺
的字段,
平铺
不包含(看不到)它

基于以下评论

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

}
Tile
中声明值时,无需再次在
TwoNTile
中声明。 您可以生成平铺对象,但不能使用构造函数

Tile t = new TwoNTile(...);
是有效的
平铺
对象。这样,您就可以实现已经尝试使用的逻辑


看看Java中的SO,或者。

您试图从没有属性的抽象类访问属性

你需要在更高的层次上进行切割

TwoNTile temp = (TwoNTile) moving;
我意识到这个抽象类可能有不止一个扩展,但是您需要将它切割到类层次结构中的最高级别,这样被铸造的类或它的后代就可以回答这个问题

更新的方法:

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

在类
Tile
中没有名为
value
的字段,您将
Tile
传递给方法,而不是
TwoNTile
,但是如果
Tile
类是
抽象的
并且无法创建对象,那么这是唯一的方法,即在
Tile
类中声明
int-value
@XP500您仍然可以从子类调用抽象类的构造函数。因此,您可以将
value
字段移动到抽象类,然后
TwoNTile
构造函数可以调用
super(value)
,但是如果
TwoNTile
扩展了
Tile
,为什么我必须这样做呢?还有别的办法吗@BlackHatSamuraibut如果我不能从
value
类中创建对象,我怎么知道
value
的值是什么@Aleksandar根据您编辑的评论,@Aleksandar,我是否应该只在
Tile
类中声明
protected int value
,就这样?@John Yes。试试看。:)您可以在TwoNTile中为该字段赋值(以及扩展平铺的其他字段),但声明是在平铺类中。