简体   繁体   English

访问超超类中的方法

[英]Accessing a method in a super super class

I want to invoke a method on the parent of a parent class. 我想在父类的父类上调用一个方法。 I am almost certain this is possible, but the following produces an error: 我几乎可以肯定这是可能的,但是以下内容会产生错误:

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
}

The compiler says AbstractUndoableEdit is not a parent class of CompoundEdit (which is wrong ): 编译器说AbstractUndoableEdit不是CompoundEdit的父类(这是错误的 ):

<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
                                                        ^

This is not possible. 这是不可能的。 The reason for that is, that it would violate encapsulation. 这样做的原因是,它将违反封装。

See why is super.super not allowed in java and similar question for scala 看看为什么在Java中不允许super.superscala的类似问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM