【发布时间】:2009-10-12 14:19:54
【问题描述】:
这段代码
class Foo(str: String) {
val len = str.length
def getLen = len
def getStr = str}
将被编译为
public class Foo implements ScalaObject
{
private final int len;
private final String str;
public Foo(String str)
{
this.str = str;
super();
len = str.length();
}
public String getStr()
{
return str;
}
public int getLen()
{
return len();
}
public int len()
{
return len;
}
public int $tag()
throws RemoteException
{
return scala.ScalaObject.class.$tag(this);
}
}
但是这段代码
class Foo(str: String) {
val len = str.length
def getLen = len
}
将被编译为
public class Foo implements ScalaObject
{
private final int len;
public Foo(String str)
{
len = str.length();
}
public int getLen()
{
return len();
}
public int len()
{
return len;
}
public int $tag()
throws RemoteException
{
return scala.ScalaObject.class.$tag(this);
}
}
为什么 Foo 类中没有私有成员?
private final String str;
这是某种优化吗?
为什么允许指向构造函数的参数。
为什么“def getStr = str”行没有编译时错误?
【问题讨论】:
-
+1 今天早上我问自己同样的事情:-)。
标签: scala