泛型类型如何对获取器和设置器起作用?

时间:2018-10-24 22:27:15

标签: java generics setter getter

我试图在使用泛型类型时为2D网格创建getter和setter,但是我在遇到错误时如何区别对待它们有些困惑。这是带有一些javadoc方法的方法,有关它们应该做什么。

public class IntGrid2D<T> implements IIntGrid2D{
private IIntPoint2D p;
private T v;
 /**
 * Sets the value at a point on the grid, replacing the previous value if any.
 * @param p The coordinate to set the value of
 * @param v The value to set at the coordinate
 * @throws OffGridException if p is outside the grid
 */
public void setPoint(IIntPoint2D p, T v) throws OffGridException{
    this.p = p;
    this.v = T;

}


/**
 * Gets the value at a point on the grid
 * @param p The coordinate to get the value of
 * @return the stored value
 * @throws OffGridException if p is outside the grid
 */
public T getPoint(IIntPoint2D p) throws OffGridException{
    return p;
}
/**
 * Gets the coordinate for the upper left most location
 * @return an IIntPoint that is the coordinate of the upper left corner
 */
public IIntPoint2D getUpperLeftCorner(){
}
/**
 * Gets the coordinate for the lower right most location
 * @return an IIntPoint that is the lower right corner
 */
public IIntPoint2D getLowerRightCorner(){
}
}

1 个答案:

答案 0 :(得分:0)

getPoint更改为此:

public T getPoint(IIntPoint2D p) throws OffGridException{
    return v;
}