访问未定义的方法

时间:2015-12-29 19:43:49

标签: java

我在另一个类中定义了以下方法build,它基本上使用了下面提到的两个类PD.javaSD.java。方法代码如下:

protected String build(final PD pd) {
    if (pd == null || pd.getField() == null) {
      return null;
    } else {
      return " ORDER BY " + pd.getFieldName() + ' ' + pd.getDirection().getCommand();
    }
  }

我的问题:

如下面PD.java所示,其中定义了getDirection()方法,但未定义getCommand()getCommand()方法在SD.java中定义。因此,我想知道在上述build方法中,以下访问方法的方式是否正确?

pd.getDirection().getCommand();

请考虑以下课程:

班级名称:SD.java

public enum SD implements MappableEnum<SD> {

  ASCENDING("A", "Ascending", "ASC"),
  DESCENDING("D", "Descending", "DESC"),

  ;

  private final String code;
  private final String description;
  private final String command;

  private static final Map<String, SD> CODE_MAP =
    MappableEnumUtil.buildCodeMap(SD.class);

  private SD(final String code, final String description, final String command) {
    this.code = code;
    this.description = description;
    this.command = command;
  }

  @JsonValue
  @Override
  public String getCode() {
    return code;
  }

  @Override
  public String getDescription() {
    return description;
  }

  @Override
  public SD toEnum(final String code) {
    return fromCode(code);
  }

  public String getCommand() {
    return command;
  }

  @JsonCreator
  public static SD fromCode(final String code) {
    return CODE_MAP.get(code);
  }

  public static SD fromCode(final char code) {
    return fromCode(String.valueOf(code));
  }

}

班级名称:PD.java

public class PD<F extends SF> {

  private final F field;

  private final Integer limit;

  private final Integer offset;

  private final SD direction;

  public PD(final F field, final Integer limit, final Integer offset, final SD direction) {
    this.field = field;
    this.limit = limit;
    this.offset = offset;
    this.direction = direction;
  }

  public F getField() {
    return field;
  }

  public String getFieldName() {
    return field.getColumnName();
  }

  public Integer getLimit() {
    return limit;
  }

  public Integer getOffset() {
    return offset;
  }

  public SD getDirection() {
    return direction;
  }
}

1 个答案:

答案 0 :(得分:1)

由于SD类型是PD类中的变量,因此它可以访问为SD类定义的任何公共方法。如果SD中方法的可见性不公开,则无法访问该方法。