Scala面向对象编程基础详解
1. 动态方法重写
在Scala中,当一个类继承另一个类并重写其中的一些方法时,需要使用 override 关键字显式声明。例如:
class BackLight() extends Light:
override def turnOn() = print("Turn on the backlights")
def turnOnStop() = print("Stop lights")
如果不使用 override 关键字重写方法,会出现编译错误:
class BackLight() extends Light:
def turnOn() = print("Turn on the backlights")
def turnOnStop() = print("Stop lights")
错误信息如下:
2 | def turnOn() = print("Turn on the backlights")
| ^
| error overriding method turnOn in class Light of type (): Unit;
| method turnOn of type (): Unit needs `override` modifier
<
超级会员免费看
订阅专栏 解锁全文
514

被折叠的 条评论
为什么被折叠?



