java - Value cannot be resolved or is not a field -
the method public boolean mergeswith(tile moving)
returns true
if this
, moving
tiles have same value. when check if same doing following:
if(this.value == temp.value){ return true; }
then shows me error on temp.value
saying value cannot resolved or not field.
how can fix that?
class twontile:
package game2048; // concrete implementation of tile. twontiles merge each other // if have same value. public class twontile extends tile { private int value; // create tile given value of n; should power of 2 // though no error checking done public twontile(int n){ value = n; } // returns true if tile merges given tile. "this" // (calling tile) assumed stationary tile while moving // presumed moving tile. twontiles merge // other twontiles same internal value. public boolean mergeswith(tile moving){ tile temp = moving; if(this.value == temp.value){ return true; } else{ return false; } } // produce new tile result of merging tile // other. twontiles, new tile twontile // , have sum of 2 merged tiles value. // throw runtime exception useful error message if // tile , other cannot merged. public tile merge(tile moving){ return null; } // score tile. score twontiles face // value. public int getscore(){ return -1; } // return string representation of tile public string tostring(){ return ""; } }
class tile:
package game2048; // abstract notion of game tile. public abstract class tile{ // returns true if tile merges given tile. public abstract boolean mergeswith(tile other); // produce new tile result of merging tile // other. may throw exception if merging illegal public abstract tile merge(tile other); // score tile. public abstract int getscore(); // return string representation of tile public abstract string tostring(); }
first: can this:
public boolean mergeswith(tile moving){ return this.value == temp.value; }
to more elegant solution.
second: need add value
variable tile
class.
public abstract class tile{ // ... // add value tile protected int value; // ... }
you have extended tile
, added new field. field not tile
's field , tile
doesn't contain (doesn't see) it.
based on comment below:
when declare value in tile
, don't need declare in twontile
again. can make tile objects, not using constructors.
tile t = new twontile(...);
is valid tile
object. way, can implement logic have tried use.
look @ static , dynamic binding in java on so, or google it.
Comments
Post a Comment