如果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();
}
答案 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;
}