Scala - 对象私有字段
本章将讲解 Scala 对象中私有字段的概念。私有字段限制了对对象内部状态的访问。因此,这些字段只能在对象本身及其定义的 class 内被访问和修改。这种封装提供了数据隐藏,并维护了对象的状态完整性。
私有字段
私有字段 是变量。这些变量在 class 和 object 中声明,不能从 class 外部直接访问。在 Scala 中,可以使用 private 关键字定义私有字段。
语法
私有字段的语法如下 -
class ClassName {
private var fieldName: Type = initialValue
}
示例
以下示例展示了如何在 Scala 编程中定义和使用私有字段。
class Point {
private var _x: Int = 0
private var _y: Int = 0
def x: Int = _x // Getter
def x_=(value: Int): Unit = { _x = value } // Setter
def y: Int = _y // Getter
def y_=(value: Int): Unit = { _y = value } // Setter
def display(): Unit = {
println(s"Point x location: $x")
println(s"Point y location: $y")
}
}
object Demo {
def main(args: Array[String]) = {
val pt = new Point
pt.x = 10
pt.y = 20
pt.display()
}
}
将上述程序保存为 Demo.scala。使用以下命令编译和执行该程序。
命令
>scalac Demo.scala >scala Demo
输出
Point x location: 10 Point y location: 20
在示例中,Point class 具有私有字段 _x 和 _y。这些字段通过公共的 getter 和 setter 方法(x、x_=、y 和 y_=)进行访问和修改。这些方法控制了对私有字段的访问。display 方法打印 x 和 y 的值。
访问修饰符
Scala 中的访问修饰符控制 class、trait、object 及其成员的可见性。private 修饰符将访问限制在定义的 class 和 object 内。
语法
私有字段的语法如下 -
class ClassName {
private var fieldName: Type = initialValue
def methodName(): Type = {
// 访问私有字段
fieldName
}
}
示例
以下示例展示了在 Scala 中使用带有访问修饰符的私有字段。
class Rectangle {
private var _width: Double = 0.0
private var _height: Double = 0.0
def width: Double = _width // Getter
def width_=(value: Double): Unit = { // Setter
if (value > 0) _width = value else println("Width must be positive")
}
def height: Double = _height // Getter
def height_=(value: Double): Unit = { // Setter
if (value > 0) _height = value else println("Height must be positive")
}
def area: Double = _width * _height
def display(): Unit = {
println(s"Rectangle width: $width")
println(s"Rectangle height: $height")
println(s"Rectangle area: $area")
}
}
object Demo {
def main(args: Array[String]) = {
val rect = new Rectangle
rect.width = 5.0
rect.height = 4.0
rect.display()
rect.width = -3.0 // 尝试设置负宽度
rect.display()
}
}
将上述程序保存为 Demo.scala。使用以下命令编译和执行该程序。
命令
>scalac Demo.scala >scala Demo
输出
Rectangle width: 5.0 Rectangle height: 4.0 Rectangle area: 20.0 Width must be positive Rectangle width: 5.0 Rectangle height: 4.0 Rectangle area: 20.0
在示例中,Rectangle class 具有私有字段 _width 和 _height。这些字段使用带有验证逻辑的公共 getter 和 setter 方法进行访问和修改。display 方法打印 width、height 和 area 的值。
伴生对象中的私有字段
Companion objects 在 Scala 中可以访问其伴生类的私有字段和方法。因此,可以在保持封装性的同时实现逻辑的清晰分离。
语法
语法如下 -
class ClassName {
private var fieldName: Type = initialValue
}
object ClassName {
def methodName(instance: ClassName): Type = {
// 访问伴生类的私有字段
instance.fieldName
}
}
示例
以下示例展示了在 Scala 编程中伴生对象中使用私有字段的情况。
class Counter {
private var count: Int = 0
def increment(): Unit = { count += 1 }
def currentCount: Int = count
}
object Counter {
def reset(counter: Counter): Unit = {
counter.count = 0 // 访问伴生类的私有字段
}
}
object Demo {
def main(args: Array[String]) = {
val counter = new Counter
counter.increment()
counter.increment()
println(s"Current count: ${counter.currentCount}") // 输出: 2
Counter.reset(counter)
println(s"Current count after reset: ${counter.currentCount}") // 输出: 0
}
}
将上述程序保存为 Demo.scala。使用以下命令来编译和执行该程序。
命令
>scalac Demo.scala >scala Demo
输出
Current count: 2 Current count after reset: 0
在示例中,Counter 类有一个私有字段 count。伴生对象 Counter 有一个 reset 方法。该方法访问并修改伴生类的私有字段 count。Demo 对象使用了 Counter 类及其伴生对象。
Scala 对象私有字段总结
- 私有字段限制了对对象内部状态的访问。因此,它提供了封装和数据隐藏。
- Scala 中的访问修饰符控制类、trait、对象及其成员的可见性。
- Getter 和 setter 提供了对私有字段的受控访问。因此,字段以受控方式被访问和修改。
- 伴生对象可以访问其伴生类的私有字段和方法。因此,可以在保持封装性的同时实现逻辑的清晰分离。