java.lang.ClassCastException:子类不能转换为超类

时间:2019-01-29 04:55:09

标签: java scala

我更改了一个实现超类接口的类,该类现在看起来像:

class TimestampConversion(format: String, formatter: SimpleDateFormat) extends ObjectConversion[Any] {
  override def execute(input: String): Timestamp = {
      // hidden implementation details
  }

  override def revert(input: Any): String = {
    // hidden implementation details
  }

  override def fromString(input: String): Timestamp = {
    // hidden implementation details
  }
}

object TimestampConversion {
  def apply(format: String): Try[TimestampConversion] = {
    // hidden implementation details
    new SimpleDateFormat().map(new TimestampConversion(format, _))
  }

}

更改代码后,调用此类时开始出现强制转换异常。此类曾经在以下时间起作用:

class TimestampConversion(format: String, formatter: SimpleDateFormat) extends ObjectConversion[Any] with FormattedConversion[SimpleDateFormat] {

  def this(format: String) = this(format, new SimpleDateFormat(format))

  override def execute(input: String): Timestamp = {
     // hidden implementation details
  }

  override def revert(input: Any): String = {
    // hidden implementation details
  }

  override def fromString(input: String): Timestamp = {
    // hidden implementation details
  }

  override def getFormatterObjects: Array[SimpleDateFormat] = {
    // hidden implementation details
  }
}

子类的层次结构为TimestampConversion(sub) <-- ObjectConversion <-- NullConversion (super)

当我尝试调用新修改的TimestampConversion类时,会出现此异常

 java.lang.ClassCastException: 
...TimestampConversion cannot be cast to 
...NullConversion
at...my_parser$$anonfun$7.apply(my_parser.scala:65)
at ...my_parser$$anonfun$7.apply(my_parser.scala:59)

此处称为TimestampConversion的地方

for ((col, i) <- all_cols) {
   val converter = col.type match {
      // hidden implementation details
      case TimestampType => TimestampConversion("some_format") // line 65 in my_parser
      case unknown => throw new RuntimeException("Met a bad type")
   }
}

它曾经被这样称呼(工作时):

for ((col, i) <- all_cols) {
   val converter = col.type match {
      // hidden implementation details
      case TimestampType => new TimestampConversion("some_format")
      case unknown => throw new RuntimeException("Met a bad type")
   }
}

我已经从@Override界面实现了两个NullConversion方法,在编译说TimestampConversion并不完全之前,我没有从IDE中得到警告或任何提示实现NullConversion之类的东西。我只是摆脱了与FormattedConversion的关联。为什么我不能像这样调用我的课程?我也从未碰过ObjectConversionNullConversion类。

以下是超类的结构:

public abstract class ObjectConversion<T> extends NullConversion<String, T> {

    public ObjectConversion() {
        super(null, null);
    }

    public ObjectConversion(T val, String nullVal) {
        super(val, nullVal);
    }

    @Override
    public T execute(String input) {
        return super.execute(input);
    }

    protected final T fromInput(String input) {
        return fromString(input);
    }

    protected abstract T fromString(String input);

    @Override
    public String revert(T input) {
        return super.revert(input);
    }

    @Override
    protected final String undo(T input) {
                // hidden implementation details
    }

    public T getValueIfStringIsNull() {
                // hidden implementation details
    }

    public String getValueIfObjectIsNull() {
                // hidden implementation details
    }

    public void setValueIfStringIsNull(T valueIfStringIsNull) {
                // hidden implementation details
    }

    public void setValueIfObjectIsNull(String valueIfObjectIsNull) {
                // hidden implementation details
    }

}

并且此类扩展了NullConversion<String, T>

public abstract class NullConversion<I, O> implements Conversion<I, O> {

    private O valueOnNullInput;
    private I valueOnNullOutput;

    public NullConversion() {
        this(null, null);
    }

    public NullConversion(O valueOnNullInput, I valueOnNullOutput) {
                // hidden implementation details
    }

    @Override
    public O execute(I input) {
                // hidden implementation details
    }

    protected abstract O fromInput(I input);

    @Override
    public I revert(O input) {
        // hidden implementation details
    }

    protected abstract I undo(O input);

    public O getValueOnNullInput() {
                // hidden implementation details
    }

    public I getValueOnNullOutput() {
                // hidden implementation details
    }

    public void setValueOnNullInput(O valueOnNullInput) {
                // hidden implementation details
    }

    public void setValueOnNullOutput(I valueOnNullOutput) {
                // hidden implementation details
    }
}

此外,这是旧代码用来扩展FormattedConversion的类(尽管我怀疑这会有所不同)

public interface FormattedConversion<T> {
   T[] getFormatterObjects();
}

0 个答案:

没有答案