scala从继承的受保护Java字段生成公共访问器
我如何才能完成以下工作:
class Foo extends javax.swing.undo.UndoManager {
// increase visibility - works for method
override def editToBeUndone: javax.swing.undo.UndoableEdit = super.editToBeUndone
// fails for field
def edits: java.util.Vector[javax.swing.undo.UndoableEdit] = super.edits
}
注意edits
是CompoundEdit
(一个UndoManager
的超类)中的受保护字段。我希望有一个与读取该字段同名的公共访问器。我该怎么做
<console>:8: error: super may be not be used on variable edits
def edits: java.util.Vector[javax.swing.undo.UndoableEdit] = super.edits
^
# 1 楼答案
不能更改继承字段的可见性,这是不允许的
在某些情况下,您可以通过使用composition来“模拟”这种行为,但显然无法实现CompoundEdit类
不确定“EditToBuEndone”,因为类中不存在此方法: http://docs.oracle.com/javase/6/docs/api/javax/swing/undo/CompoundEdit.html
# 2 楼答案
嗯,总是有反思的
您还可以通过嵌套消除这两个调用的歧义,尽管这很难看:
您确实需要
()
,但如果没有它,它会与字段本身冲突(并且您不能用def
覆盖val
)