简体   繁体   中英

How do generic types work for getters and setters?

I am trying to create getters and setters for a 2D grid while using generic types, but I'm somewhat confused in how to treat them differently as I'm getting errors. Here are the methods with some javadoc on what they should be doing.

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(){
}
}

Change getPoint to this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM