1

我有一个扩展类DataRow

import org.jdesktop.dataset.DataRow;

public class MainDataRow extends DataRow {
  private MainDataTable baseDataTable;

  protected MainDataRow(MainDataTable dt) {
    super(dt);
    this.baseDataTable = dt;
  }

  public int    getId()                           { return (int)    super.getValue(baseDataTable.getColId()); };
  public void   setId(int id)                     {                 super.setValue(baseDataTable.getColId(), id); };
  public int    getDelta()                        { return (int)    super.getValue(baseDataTable.getColDelta()); };
  public void   setDelta(int delta)               {                 super.setValue(baseDataTable.getColDelta(), delta); };
  public String getNombre()                       { return (String) super.getValue(baseDataTable.getColNombre()); };
  public void   setNombre(String nombre)          {                 super.setValue(baseDataTable.getColNombre(), nombre); };

MainDataTable扩展DataTable, 并返回getColId(), getColDelta(),的有效列getColNombre()

我想要做:

MainDataTable dt = new MainDataTable(ds);
MainDataRow dr = (MainDataRow) dt.appendRow();

但这是不可能的,因为CastClassException( dt.appendRowreturn DataRowand MainDataRowis extended DataRow,反之亦然,所以唯一的可能性可能是类似于DataRow dr = (DataRow) new MainDataRow(dt);)。

在 c++ 中,它可以通过以下方式轻松实现DataRowBuilder,覆盖NewRowFromBuilder()MainDataTable覆盖受保护的创建者DataRowBuilderMainDataRowDataRow 转换为强类型 DataRow:他们是如何做到的?)。

我怎么能在Java中做到这一点?

编辑

MainDataTable 类:

public class MainDataTable extends TypedDataTable<MainDataRow> {
...
}

和 TypedDataTable 类:

public abstract class TypedDataTable<TypeOfRow> extends DataTable {
  protected boolean locked;

  public TypedDataTable(DataSet ds, boolean appendRowSupported) {
    super(ds);
    InitClass();
    super.setAppendRowSupported(appendRowSupported);
    locked = false;
  }

  public Object clone() {
    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
      return null;
    }
  }

  @Override
  public abstract DataRow appendRow();
  @Override
  public abstract DataRow appendRowNoEvent();
  public abstract void InitVars();
  public abstract void InitClass();

  public boolean isLocked() {
    return locked;
  }

  public void setLocked(boolean locked) {
    this.locked = locked;
  }
}
4

1 回答 1

1

覆盖appendRow()appendRowNoEvent()在 MainDataTable 中返回 MainDataRow

public abstract class TypedDataTable<TypeOfRow extends DataRow> extends DataTable {
  protected boolean locked;

  public TypedDataTable(DataSet ds, boolean appendRowSupported) {
    super(ds);
    InitClass();
    super.setAppendRowSupported(appendRowSupported);
    locked = false;
  }

  public Object clone() {
    try {
      return super.clone();
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
      return null;
    }
  }

 @Override
 public TypeOfRow appendRowNoEvent() {
     if (appendRowSupported) {
         TypeOfRow row = createDataRow(); //<-- HERE we create a MainDataRow!!!
         rows.add(row);
         return row;
     } else {
         return null;
     }
 }

 @Override
 public TypeOfRow appendRow() {
      return (TypeOfRow)super.appendRow();
 }

  public abstract TypeOfRow createDataRow();
  public abstract void InitVars();
  public abstract void InitClass();

  public boolean isLocked() {
    return locked;
  }

  public void setLocked(boolean locked) {
    this.locked = locked;
  }
}
于 2012-05-07T12:19:05.730 回答