我是 Scala 新手,需要澄清以下涉及类构造函数的代码片段。
class sample (a: Int, b: Int) {
/* define some member functions here */
}
我可以认为变量 a
和 b
是类 sample
私有(private)的吗?
class sample (val a: Int, val b: Int) {
/* define some member functions here */
}
在这种情况下,a
和 b
可以公开访问吗?在构造函数的参数列表中添加 val
关键字的具体效果是什么?如果我使用 def
关键字而不是 val
,它是否也具有相同的效果?
最佳答案
以下 Scala 类显示了构造函数参数的四种可能性(3 种不同的声明,其中一种有两种效果):
class ConstructorParams (local:Int, prvt:Int, val readonly:Int, var writable:Int) {
def succ_prvt() = prvt + 1
}
- 第一个参数
local
未被任何方法引用。它仅作为构造函数中的局部变量存在(它可以由字段初始值设定项引用,而无需更改它;尝试将val foo = local
添加到构造函数中)。 - 第二个
prvt
由方法引用,因此它成为私有(private)字段。 - 第三个的
val
声明符创建一个 getter 和一个私有(private)支持字段。 - 第四个的
var
声明符创建了一个 getter 和一个 setter,以及一个私有(private)支持字段。
特别要注意,没有 val
或 var
声明符的构造函数参数只有在该函数内的函数中引用时才是私有(private)的(这是可访问性的问题)类(class);否则,它是本地的(这是范围问题)。
从技术上讲,最后三个参数作为本地值和私有(private)字段(初始化为本地值)存在,但这种区别不应该出现太多,所以你基本上可以把它排除在外。
def
作为参数声明符没有多大意义,因为它用于引入新函数,而不是声明值/变量名称;它也不用于函数参数(构造函数参数与函数参数密切相关)。由于函数是第一类,因此您可以使用函数类型而不是特殊声明符来声明参数包含函数。
通过将 -Xprint:constructors
传递给编译器,打印出编译器对 ConstructorParams
的构成,我们得到(添加了注释):
class ConstructorParams extends Object {
/* 2nd (no-declarator) param becomes private field due to reference in `succ_prvt()` */
<paramaccessor> private[this] val prvt: Int = _;
/* `val` becomes private field + getter */
<paramaccessor> private[this] val readonly: Int = _;
<stable> <accessor> <paramaccessor> def readonly(): Int = ConstructorParams.this.readonly;
/* `var` becomes private field + getter + setter */
<paramaccessor> private[this] var writable: Int = _;
<accessor> <paramaccessor> def writable(): Int = ConstructorParams.this.writable;
<accessor> <paramaccessor> def writable_=(x$1: Int): Unit = ConstructorParams.this.writable = x$1;
/* causes `prvt` constructor param to become private field */
def succ_prvt(): Int = ConstructorParams.this.prvt.+(1);
def <init>(local: Int, prvt: Int, readonly: Int, writable: Int): ConstructorParams = {
ConstructorParams.this.prvt = prvt;
ConstructorParams.this.readonly = readonly;
ConstructorParams.this.writable = writable;
ConstructorParams.super.<init>();
()
}
}
上面的示例类编译为 Java 等效项:
public class ConstructorParams {
/* 2nd (no-declarator) param becomes private field due to reference in `succ_prvt()` */
private final int prvt;
public int succ_prvt() {
return this.prvt + 1;
}
/* `val` becomes private field + getter */
private final int readonly;
public int readonly() { return this.readonly; }
/* `var` becomes private field + getter + setter */
private int writable;
public int writable() { return this.writable; }
public void writable_$eq(int x$1) {
this.writable = x$1;
}
/* 1st param is local, since it's not referenced in any other methods */
public ConstructorParams(int local, int prvt, int readonly, int writable) {
/* parent constructor is invoked implicitly, so not repeated here */
this.prvt = prvt;
this.readonly = readonly;
this.writable = writable;
}
}
如果您在 ConstructorParams 上使用 javap
(就像 Brian 所做的那样)以及 -p
参数,您将看到一个与上述 Java 源代码中的类签名等效的类签名.
关于scala - 需要澄清 scala 中的构造函数定义语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12757616/