覆盖Trait内部Object内的函数

时间:2015-05-14 00:14:23

标签: scala override

说我有这样的事情:

trait A {
  object B {
    def doSomething = "test"
  }
}

class C extends A {
   def out = print(B.doSomething)
}

class D extends A {
   // override B.doSomething
}

如何覆盖doSomething内的函数object B

1 个答案:

答案 0 :(得分:2)

这有点重复,但有两个不同的问题:

首先,objects are not meant to be overridden。第二,inheriting from a nested class is somewhat straightforward

Thread : Fatal Exception: NSRangeException
0  CoreFoundation                 0x00000001830502d8 __exceptionPreprocess + 132
1  libobjc.A.dylib                0x00000001948740e4 objc_exception_throw + 60
2  CoreFoundation                 0x0000000182f3385c -[__NSArrayM removeObjectAtIndex:]
3  AppName                        0x00000001000875b8 
4  AppName                        0x000000010008698c 
5  AppName                        0x00000001000867c4 
6  libdispatch.dylib              0x0000000194ec5994 _dispatch_call_block_and_release + 24
7  libdispatch.dylib              0x0000000194ec5954 _dispatch_client_callout + 16
8  libdispatch.dylib              0x0000000194eca20c _dispatch_main_queue_callback_4CF + 1608
9  CoreFoundation                 0x00000001830077f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
10 CoreFoundation                 0x00000001830058a0 __CFRunLoopRun + 1492
11 CoreFoundation                 0x0000000182f312d4 CFRunLoopRunSpecific + 396
12 GraphicsServices               0x000000018c7476fc GSEventRunModal + 168
13 UIKit                          0x0000000187af6fac UIApplicationMain + 1488
14 AppName                        0x000000010012e8cc 
15 libdyld.dylib                  0x0000000194ef2a08 start + 4