访问超级超类中的方法

时间:2014-05-11 21:27:05

标签: scala superclass super

我想在父类的父级上调用一个方法。我几乎可以肯定这是可能的,但是下面会产生错误:

import javax.swing.undo._

def test(name: String): CompoundEdit = new CompoundEdit {
  override def getPresentationName    : String = name

  override def getUndoPresentationName: String = 
    super[AbstractUndoableEdit].getUndoPresentationName

  override def getRedoPresentationName: String = 
    super[AbstractUndoableEdit].getRedoPresentationName
}

编译器说AbstractUndoableEdit不是CompoundEdit的父类(wrong):

<console>:55: error: AbstractUndoableEdit does not name a parent class of <$anon: javax.swing.undo.CompoundEdit>
         override def getUndoPresentationName: String = super[AbstractUndoableEdit].getUndoPresentationName
                                                        ^
<console>:56: error: AbstractUndoableEdit does not name a parent class of <$anon: javax.swing.undo.CompoundEdit>
         override def getRedoPresentationName: String = super[AbstractUndoableEdit].getRedoPresentationName
                                                        ^

1 个答案:

答案 0 :(得分:4)

这是不可能的。原因是,它会违反封装。

请参阅why is super.super not allowed in javasimilar question for scala