根据this highly voted answer on SO,Python必须在实例方法中声明self
,因为语言设计者希望通过this
来阻止通过Java完成实例化对象的推导。
“this
的含义是什么意思可以用Java推断出来?”
答案 0 :(得分:3)
如果在Java中引用没有限定符的标识符(例如something
),编译器将检查是否存在具有该名称的局部变量,如果没有,则推断它必须是字段
答案 1 :(得分:2)
我认为最初的帖子意味着在某些情况下,Java需要使用'this'来明确你所指的变量。例如:
public class A {
private String b;
private String c;
public void setB(String b) {
this.b = b; // <-- this removes confusion for Java
}
public void setC(String _c) {
c = _c; // <-- this is not needed here
}
}