Scala - 构造函数
本章将带您了解如何在 Scala 编程中使用构造函数。构造函数是用于初始化对象的特殊方法。在 Scala 中,有两种构造函数:主构造函数和辅助构造函数。理解如何使用构造函数可以创建健壮且易于维护的代码。
Scala 中的构造函数
在 Scala 中,构造函数用于在创建对象时初始化该对象。主构造函数是 class 签名的一部分。而辅助构造函数则在 class 主体内定义。
1. 主构造函数
主构造函数是 class 的主要构造函数。主构造函数作为 class 签名的一部分定义。当实例化 class 的对象时,它会被执行。
语法
以下是 Scala 编程中主构造函数的语法 -
class ClassName(parameter1: Type, parameter2: Type) {
// 类主体
}
示例
以下示例展示了在 Scala 中如何使用简单的辅助构造函数。
class Point(val xc: Int, val yc: Int) {
var x: Int = xc
var y: Int = yc
def move(dx: Int, dy: Int): Unit = {
x += dx
y += dy
}
override def toString: String = s"Point($x, $y)"
}
object Demo {
def main(args: Array[String]): Unit = {
val point = new Point(10, 20)
println(point)
point.move(5, 5)
println(point)
}
}
将上述程序保存为 Demo.scala。使用以下命令编译并执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Point(10, 20) Point(15, 25)
在示例中,Point class 有一个带有参数 xc 和 yc 的主构造函数。Demo object 展示了如何创建 Point class 的实例并调用其方法。
2. 辅助构造函数
辅助构造函数是使用 this 关键字在 class 主体内定义的额外构造函数。这些是创建 class 实例的替代方式。
语法
以下是 Scala 编程中辅助构造函数的语法 -
class ClassName(parameter1: Type, parameter2: Type) {
// 主构造函数
def this(parameter1: Type) {
this(parameter1, defaultValue)
// 额外初始化
}
// 类主体
}
示例
以下示例展示了在 Scala 编程中使用辅助构造函数。
class Rectangle(val width: Int, val height: Int) {
var area: Int = width * height
def this(side: Int) = {
this(side, side)
}
override def toString: String = s"Rectangle(width: $width, height: $height, area: $area)"
}
object Demo {
def main(args: Array[String]): Unit = {
val rectangle1 = new Rectangle(10, 20)
val rectangle2 = new Rectangle(15)
println(rectangle1)
println(rectangle2)
}
}
将上述程序保存为 Demo.scala。使用以下命令编译并执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Rectangle(width: 10, height: 20, area: 200) Rectangle(width: 15, height: 15, area: 225)
在示例中,Rectangle class 有一个主构造函数和一个辅助构造函数。辅助构造函数使用单个边长创建正方形。Demo object 展示了如何使用两种构造函数创建 Rectangle class 的实例。
构造函数重载
构造函数重载是指定义多个具有不同参数列表的构造函数的过程。这样,你就可以以不同的方式实例化 class。
语法
以下是 Scala 编程中构造函数重载的语法 -
class ClassName {
def this(parameter1: Type) {
// 构造函数体
}
def this(parameter1: Type, parameter2: Type) {
// 构造函数体
}
}
示例
以下示例演示了在 Scala 编程中使用的构造函数重载。
class Circle(radius: Double, val color: String) {
def this(radius: Double) = {
this(radius, "red")
}
def this() = {
this(1.0, "red")
}
def area: Double = Math.PI * radius * radius
override def toString: String = s"Circle(radius: $radius, color: $color, area: $area)"
}
object Demo {
def main(args: Array[String]): Unit = {
val circle1 = new Circle(5.0, "blue")
val circle2 = new Circle(10.0)
val circle3 = new Circle()
println(circle1)
println(circle2)
println(circle3)
}
}
将上述程序保存为 Demo.scala。使用以下命令来编译和执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Circle(radius: 5.0, color: blue, area: 78.53981633974483) Circle(radius: 10.0, color: red, area: 314.1592653589793) Circle(radius: 1.0, color: red, area: 3.141592653589793)
在示例中,Circle class 具有多个构造函数,以提供对象创建的灵活性。Demo object 使用不同的构造函数实例化 Circle class。
构造函数中的默认参数
在 Scala 中,你可以为构造函数参数定义默认值。这样,当某些参数未提供时,就可以使用默认值创建对象。
语法
以下是 Scala 编程中构造函数默认参数的语法 -
class ClassName(parameter1: Type = defaultValue1, parameter2: Type = defaultValue2) {
// Class 体
}
示例
以下示例展示了 Scala 中构造函数的默认参数。
class Car(val brand: String = "Toyota", val model: String = "Corolla", val year: Int = 2020) {
override def toString: String = s"Car(brand: $brand, model: $model, year: $year)"
}
object Demo {
def main(args: Array[String]): Unit = {
val car1 = new Car()
val car2 = new Car("Honda", "Civic")
val car3 = new Car("Ford", "Mustang", 2022)
println(car1)
println(car2)
println(car3)
}
}
将上述程序保存为 Demo.scala。使用以下命令来编译和执行该程序。
命令
> scalac Demo.scala > scala Demo
输出
Car(brand: Toyota, model: Corolla, year: 2020) Car(brand: Honda, model: Civic, year: 2020) Car(brand: Ford, model: Mustang, year: 2022)
在示例中,Car class 的构造函数参数具有默认值。Demo object 创建 Car class 的实例时,既可以指定所有参数,也可以不指定。
Scala 构造函数总结
- Scala 中的构造函数是用于初始化对象的特殊方法。
- 主构造函数是 class 签名的一部分,在创建对象时执行。
- 辅助构造函数提供使用 this 关键字 创建 class 实例的额外方式。
- 构造函数重载可以定义具有不同参数列表的多个构造函数。
- 当某些参数未提供时,可以指定默认值。