[英]Value cannot be resolved or is not a field
The method public boolean mergesWith(Tile moving)
returns true
if this
and moving
tiles have the same value. 如果
this
和moving
切片具有相同的值,则方法public boolean mergesWith(Tile moving)
将返回true
。 But when I check if they are the same by doing the following: 但是当我通过执行以下操作检查它们是否相同时:
if(this.value == temp.value){
return true;
}
then it shows me error on temp.value
saying that value cannot be resolved or is not a field . 然后它显示
temp.value
错误,表示无法解析值或不是字段 。
How can I fix that? 我该如何解决这个问题?
Class TwoNTile : 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();
}
First : You can do this: 第一 :你可以这样做:
public boolean mergesWith(Tile moving){
return this.value == temp.value;
}
to get more elegant solution. 获得更优雅的解决方案。
Second : You need to add value
variable to Tile
class. 第二 :您需要将
value
变量添加到Tile
类。
public abstract class Tile{
// ...
// add a value to Tile
protected int value;
// ...
}
You have extended Tile
and added new field. 您已扩展
Tile
并添加了新字段。 That field is not Tile
's field and Tile
doesn't contain (doesn't see) it. 那个字段不是
Tile
的字段,而Tile
不包含(看不到)它。
Based on the comment below : 根据以下评论 :
When you declare value in Tile
, you don't need to declare it in TwoNTile
again. 在
Tile
声明值时,不需要再次在TwoNTile
声明它。 You can make Tile objects, just not by using constructors. 你可以制作 Tile对象,而不是使用构造函数。
Tile t = new TwoNTile(...);
is a valid Tile
object. 是一个有效的
Tile
对象。 This way, you can implement logic you have already tried to use. 这样,您就可以实现已经尝试过的逻辑。
Look at static and dynamic binding in Java on SO, or google it . 在SO上查看Java中的静态和动态绑定 ,或者google它 。
You are trying to access the property from the abstract class that does not have it. 您正尝试从没有它的抽象类访问该属性。
You need to cut it at higher level eg 你需要在更高的水平上削减它,例如
TwoNTile temp = (TwoNTile) moving;
I realize that there might be more than one extensions of this abstract class but you need to cut it to the highest level in class hierarchy such that the casted class or it's decedents can answer this. 我意识到这个抽象类可能有多个扩展,但是你需要将它切换到类层次结构中的最高级别,这样才能使得类或者它的后代可以回答这个问题。
Updated method: 更新方法:
public boolean mergesWith(Tile moving){
TwoNTile temp = (TwoNTile) moving;
if(this.value == temp.value){
return true;
}
else{
return false;
}
}
In your Tile
class, you need to add the class variable value
, like so: 在你的
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.