【问题标题】:Value cannot be resolved or is not a field值无法解析或不是字段
【发布时间】:2015-07-14 00:11:36
【问题描述】:

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

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

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

我该如何解决这个问题?

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

}

【问题讨论】:

  • Tile 类中没有名为 value 的字段,您将 Tile 传递给方法,而不是 TwoNTile
  • 但是如果Tile 类是abstract 并且不能创建对象,这是唯一的方法吗,即在Tile 类中声明一个int value? @xp500
  • 您仍然可以从子类中调用抽象类的构造函数。所以可以将value字段移到抽象类中,然后TwoNTile构造函数就可以调用super(value)

标签: java


【解决方案1】:

首先:您可以这样做:

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

获得更优雅的解决方案。

第二个:需要在Tile类中添加value变量。

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

【讨论】:

  • 但是如果我不能从中创建对象,我怎么知道valueTile 类中的值是多少? @亚历山大
  • 根据您编辑的 cmets,@Aleksandar,我应该只在 Tile 类中声明 protected int value,就这样吗?
  • @John 是的。试试看。 :) 您在 TwoNTile(以及其他扩展 Tile)中为该字段分配了一个值,但声明在 Tile 类中。
【解决方案2】:

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

你需要在更高的层次上切割它,例如

TwoNTile temp = (TwoNTile) moving;

我意识到这个抽象类可能有不止一个扩展,但你需要将它削减到类层次结构中的最高级别,以便被强制转换的类或其后继者可以回答这个问题。

更新方法:

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

【讨论】:

    【解决方案3】:

    在您的Tile 类中,您需要添加类变量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();
    
    }
    

    【讨论】:

    • 但是如果TwoNTile 扩展Tile 为什么我必须这样做?还有其他方法吗? @BlackHatSamurai
    猜你喜欢
    • 2022-01-09
    • 2014-03-27
    • 2014-01-05
    • 2016-05-20
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多