在 Scala 中为对象字段自动生成 getter/setter 的确切意义是什么? [英] What is exactly the point of auto-generating getters/setters for object fields in Scala?

查看:43
本文介绍了在 Scala 中为对象字段自动生成 getter/setter 的确切意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我们所知,Scala 会自动为任何公共字段生成 getter 和 setter,并将实际字段变量设为私有.为什么比仅仅公开该领域更好?

As we know, Scala generates getters and setters automatically for any public field and make the actual field variable private. Why is it better than just making the field public ?

推荐答案

首先,这允许将公共 var/val 与(几个)def(s) 交换,并且仍然保持二进制兼容性.其次,它允许覆盖派生类中的 var/val.

For one this allows swapping a public var/val with a (couple of) def(s) and still maintain binary compatibility. Secondly it allows overriding a var/val in derived classes.

这篇关于在 Scala 中为对象字段自动生成 getter/setter 的确切意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆