왜 생성자 매개 변수를 가리키는 것이 허용됩니까? 어떤 개인이 왜
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 오늘 아침에 나는 똑같은 질문을하고 있었다 :-). – helpermethod