我从 Python 来到 Java 并认为这this
基本上就像 Python 的self
......但是这个小代码让我感到困惑。从功能上讲,这段代码:
public class Test {
private String name;
public Test(String givenName)
{
this.name = givenName;
}
public String nameGet()
{
return this.name;
}
public static void main(String[] args)
{
Test example = new Test("Hello Guys");
System.out.println(example.nameGet());
}
}
与此代码执行相同的操作:
public class Test {
private String name;
public Test(String givenName)
{
name = givenName;
}
public String nameGet()
{
return name;
}
public static void main(String[] args)
{
Test example = new Test("Hello Guys");
System.out.println(example.nameGet());
}
}
既然这样,请原谅双关语,似乎是这样,那么在课堂上工作时提到这个有什么意义呢?