Scala - 方法重写
本章将带您了解 Scala 编程中的方法重写。子类可以为其超类中已定义的方法提供特定的实现。
方法重写
子类可以为其超类中已定义的方法提供特定的实现。因此,子类可以修改和扩展该方法的行为。您可以实现运行时多态,其中单一接口可用于不同的底层形式(数据类型)。
方法重写是指子类中的方法与超类中的方法具有相同的名称、返回类型和参数,但提供不同的实现。
语法
方法重写的语法如下 -
class SuperClass {
def methodName(param1: Type1): ReturnType = {
// 超类实现
}
}
class SubClass extends SuperClass {
override def methodName(param1: Type1): ReturnType = {
// 子类实现
}
}
示例
以下示例演示了 Scala 编程中的方法重写 -
class Animal {
def sound(): Unit = {
println("Animal makes a sound")
}
}
class Dog extends Animal {
override def sound(): Unit = {
println("Dog barks")
}
}
object Demo {
def main(args: Array[String]): Unit = {
val animal: Animal = new Dog
animal.sound()
}
}
将上述程序保存为 Demo.scala。使用以下命令编译并执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Dog barks
在示例中,Dog 类使用 override 关键字重写了 Animal 类的 sound 方法。Demo 对象通过创建 Dog 的实例但使用 Animal 类型引用它来展示多态,并调用了重写后的 sound 方法。
方法重写的规则
1. override 关键字
重写方法时必须使用 override 关键字。这样可以避免意外重写并保持代码的可读性。
2. 方法签名
子类中的方法签名必须与超类中的方法签名完全匹配,包括方法名称、返回类型和参数列表。
3. 访问修饰符
重写方法的访问级别必须与超类中的方法相同或更不严格。
不同参数列表的方法重写
在重写方法时,子类可以拥有不同参数列表的方法。但要进行重写,方法签名必须与超类方法完全匹配。
示例
以下示例展示了 Scala 编程中不同参数列表的方法重写 -
class Vehicle {
def start(): Unit = {
println("Vehicle is starting")
}
def start(key: String): Unit = {
println(s"Vehicle is starting with key: $key")
}
}
class Car extends Vehicle {
override def start(): Unit = {
println("Car is starting")
}
override def start(key: String): Unit = {
println(s"Car is starting with key: $key")
}
}
object Demo {
def main(args: Array[String]): Unit = {
val vehicle = new Vehicle()
val car = new Car()
vehicle.start() // Vehicle is starting
vehicle.start("VehicleKey") // Vehicle is starting with key: VehicleKey
car.start() // Car is starting
car.start("CarKey") // Car is starting with key: CarKey
}
}
将上述程序保存为 Demo.scala。使用以下命令编译并执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Vehicle is starting Vehicle is starting with key: VehicleKey Car is starting Car is starting with key: CarKey
在示例中,Car 类重写了 Vehicle 类的 start 方法。Demo 对象展示了如何在 Vehicle 和 Car 的实例上调用被重写的方法。
使用协变返回类型的重写
可以使用协变返回类型进行方法重写,其中重写方法的返回类型是被重写方法的返回类型的子类型。
示例
以下示例展示了 Scala 编程中协变返回类型的方法重写 -
class Animal {
def create(): Animal = {
println("Creating an Animal")
new Animal
}
}
class Dog extends Animal {
override def create(): Dog = {
println("Creating a Dog")
new Dog
}
}
object Demo {
def main(args: Array[String]): Unit = {
val animal = new Animal()
val dog = new Dog()
val newAnimal: Animal = animal.create() // Creates an Animal
val newDog: Dog = dog.create() // Creates a Dog
}
}
将上述程序保存为 Demo.scala。使用以下命令编译并执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Creating an Animal Creating a Dog
在示例中,Dog 类使用协变返回类型重写了 Animal 类的 create 方法。Demo 对象展示了如何在 Animal 和 Dog 的实例上调用被重写的方法。
使用超类方法
可以使用 super 关键字调用被重写方法的超类版本。这样就可以使用 super 关键字访问超类方法。
示例
以下示例展示了如何使用 super 关键字访问超类方法 -
class Animal {
def sound(): Unit = {
println("Animal makes a sound")
}
}
class Dog extends Animal {
override def sound(): Unit = {
super.sound()
println("Dog barks")
}
}
object Demo {
def main(args: Array[String]): Unit = {
val dog = new Dog
dog.sound()
}
}
将上述程序保存为 Demo.scala。使用以下命令编译并执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Animal makes a sound Dog barks
在示例中,Dog 类重写了 Animal 类的 sound 方法。它使用 super 关键字在执行自己的实现之前调用超类版本的 sound 方法。
方法重写总结
- 子类为超类中定义的方法提供特定实现。
- 重写的方法必须具有与它们重写的方法相同的签名。
- 方法重写支持多态、代码复用性和动态绑定。
- 可以使用协变返回类型进行方法重写,其中重写方法的返回类型是重写方法返回类型的子类型。
- 子类中的方法签名必须与超类中的方法签名匹配。
- 可以使用 super 关键字调用超类版本的重写方法。
- 为了清晰并避免错误,应使用 override 关键字。