我有一个扩展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.appendRow返回DataRow并且MainDataRow扩展了DataRow,反之亦然,因此唯一的可能性可能是与DataRow dr = (DataRow) new MainDataRow(dt);类似的东西)。

在c ++中,可以通过DataRowBuilder,在NewRowFromBuilder()中覆盖MainDataTable并从DataRowBuilderCasting DataRow to Strongly-Typed DataRow: How do they do it?)中的MainDataRow覆盖受保护的创建者来轻松实现。

如何用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;
  }
}

最佳答案

覆盖MainDataTable中的appendRow()appendRowNoEvent()以返回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;
  }
}

07-24 09:15