Swing 类的子对象中的scala重写方法
scala新手,似乎找不到这种情况的参考资料 我试图覆盖scala.swing.TabbedPane.pages上的一些方法: 此类的定义为:Swing 类的子对象中的scala重写方法,swing,scala,Swing,Scala,scala新手,似乎找不到这种情况的参考资料 我试图覆盖scala.swing.TabbedPane.pages上的一些方法: 此类的定义为: class TabbedPane extends Component with Publisher { object pages extends BufferWrapper[Page] { def += } } 我无法理解重写InternalPages对象中任何方法的语法。scala可以这样做吗 谢谢 编辑:使这个问题更清
class TabbedPane extends Component with Publisher {
object pages extends BufferWrapper[Page] {
def +=
}
}
我无法理解重写InternalPages对象中任何方法的语法。scala可以这样做吗
谢谢
编辑:使这个问题更清楚
class CloseableTabbedPane extends TabbedPane{
}
val pane = new CloseableTabbedPane;
pane.pages += new CloseablePage;
我需要重写方法窗格。pages+=接受带有图标的可关闭页面。在重写类中的方法的正常情况下:
scala> class Foo { def foo : String = "Foo" }
defined class Foo
scala> class Bar extends Foo { override def foo : String = "Bar" }
defined class Bar
scala> val b = new Bar
b: Bar = Bar@1d2bb9f
scala> b.foo
res0: String = Bar
但是,您已经询问是否可以覆盖对象:
scala> class FooBar {
| object prop extends Foo {
| def foo2 : String = "foo2"
| }
| }
defined class FooBar
scala> val fb = new FooBar
fb: FooBar = FooBar@183d59c
scala> fb.prop.foo
res1: String = Foo
现在进行覆盖:
scala> fb.prop.foo2
res2: String = foo2
scala> class FooBaz extends FooBar {
| override object prop extends Bar {
| def foo2 : String = "bar2"
| }
| }
<console>:8: error: overriding object prop in class FooBar of type object FooBaz.this.prop;
object prop cannot be used here - classes and objects cannot be overridden
override object prop extends Bar {
^
scala>fb.prop.foo2
res2:String=foo2
scala>类FooBaz扩展了FooBar{
|覆盖对象属性扩展栏{
|def foo2:String=“bar2”
| }
| }
:8:错误:重写对象FooBaz.this.prop类型的FooBar类中的对象prop;
此处不能使用对象属性-不能重写类和对象
覆盖对象属性扩展栏{
^
这真的没有意义,因为您如何确保重写的值也扩展了Foo?重写普通类方法不是问题所在,抱歉,我没有说得更清楚。它重写的是对象“页面”中存在的方法这存在于类选项卡窗格中。我同意这是没有意义的,但我想知道为什么会有人制作这样的API,因为它很难覆盖功能。也许有一种更简单的方法来完成我试图完成的任务?是的-这正是我在思考的Swing实现的类型-它是不完整;您必须修补现有类以允许
Page
具有已定义的图标并仅使用该图标。您是否尝试将override关键字放在对象页面中的def前面?