我有一个扩展Trait的类,我想编写可以与类混合的Traits并覆盖一些方法。
我的课程所延伸的特性看起来像这样
int progress = 70;
imageView.setImageBitmap(getCroppedBitmap(originalPhotoBitmap, progress));
扩展CharSource的类
trait CharSource {
def source: Iterator[Char]
def words: Iterator[String] = {
while(source.hasNext){
//Logic to get the next word from a Char Iterator
}
}
final def chars: Iterator[Char] = words.toString().toIterator
}
现在我想写一个特性来覆盖IteratorDocutmentSource中的源def以获得一些特殊行为
class IteratorDocumentSource(iterator: Iterator[Char]) extends CharSource {
def source = iterator
}
PunctioationRemover扩展的变换器特性
trait PunctuationRemover extends Transformer {self: CharSource =>
override
/** Character source. Overriding should be in terms of `super.source` for
stackability. */
abstract val source: Iterator[Char] = {
super.source
//additional logic ...
}
}
拨打电话时出错
trait Transformer {self: CharSource =>
protected def source: Iterator[Char]
def words: Iterator[String]
}
错误:
异常或错误导致运行中止:
SampleSuite $$ anonfun $ $$ 2不久$ $ 3.document $$ PunctuationRemover超级$源()Lscala /收集/迭代器; java.lang.AbstractMethodError: SampleSuite $$ anonfun $ $$ 2不久$ $ 3.document $$ PunctuationRemover超级$源()Lscala /收集/迭代;
我引用了这篇文章,但我认为我的情况有点不同 Can I override a scala class method with a method from a trait?