String graph = null; FileWriter fw = new FileWriter(graph+".sh"); for(int j = 0;j<list.size();j++){ this.graph=props.getProperty("graph"+String.valueOf(i)); }
Java关键字this只能用于方法方法体内。
当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是 this。
因此,this只能在类中的非静态方法中使用,静态方法和静态的代码块中绝对不能出现this,
并且this只和特定的对象关联,而不和类关联,同一个类的不同对象有不同的this。
例如:
public class Test
{
public String graph = null;
public void demo()
{
FileWriter fw = new FileWriter(graph+".sh");
for(int j = 0;j<list.size();j++){
this.graph=props.getProperty("graph"+String.valueOf(i));
}
}
这个 graph 作为类的成员变量就可以this了
this 在java中指定是当前类的实例。this只能调用类成员。
这个this就是当前类把,你当前类应该是不能访问你方法里面的变量把
为什么要这样写呢?直接 graph=props.getProperty("graph"+String.valueOf(i)); 不就可以了吗?没必要在前面加个this