[英]Value cannot be resolved or is not a field
如果this
和moving
切片具有相同的值,則方法public boolean mergesWith(Tile moving)
將返回true
。 但是當我通過執行以下操作檢查它們是否相同時:
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 "";
}
}
類瓦片 :
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();
}
第一 :你可以這樣做:
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對象,而不是使用構造函數。
Tile t = new TwoNTile(...);
是一個有效的Tile
對象。 這樣,您就可以實現已經嘗試過的邏輯。
您正嘗試從沒有它的抽象類訪問該屬性。
你需要在更高的水平上削減它,例如
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
,如下所示:
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.