我更改了一个实现超类接口的类,该类现在看起来像:
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
的关联。为什么我不能像这样调用我的课程?我也从未碰过ObjectConversion
和NullConversion
类。
以下是超类的结构:
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();
}