在我的AP计算机科学课上,我们一直在进行GridWorld案例研究.在查看它时,似乎AbstractGrid类没有理由是抽象的,因为它没有抽象的方法或值.为什么会这样?
更多信息
如果只是为了强制实现Grid接口,为什么不将这些方法作为抽象方法(因此在没有接口的情况下强制这些类的签名).而且,无论如何,这两个孩子都会覆盖它的大部分方法.
package info.gridworld.grid;
import java.util.ArrayList;
public abstract class AbstractGrid<E> implements Grid<E>
{
public ArrayList<E> getNeighbors(Location loc)
{
ArrayList<E> neighbors = new ArrayList<E>();
for (Location neighborLoc : getOccupiedAdjacentLocations(loc))
neighbors.add(get(neighborLoc));
return neighbors;
}
public ArrayList<Location> getValidAdjacentLocations(Location loc)
{
ArrayList<Location> locs = new ArrayList<Location>();
int d = Location.NORTH;
for (int i = 0; i < Location.FULL_CIRCLE / Location.HALF_RIGHT; i++)
{
Location neighborLoc = loc.getAdjacentLocation(d);
if (isValid(neighborLoc))
locs.add(neighborLoc);
d = d + Location.HALF_RIGHT;
}
return locs;
}
public ArrayList<Location> getEmptyAdjacentLocations(Location …Run Code Online (Sandbox Code Playgroud)