JAVA中关于“ this”关键字的问题

时间:2019-12-30 01:37:42

标签: java this keyword

class S2{  
  void m(S2 obj){  
    System.out.println("method is invoked");  
  }  
  void p(){  
    m(this); //instead of this keyword what can i use?  
  }  
  public static void main(String args[]){  
    S2 s1 = new S2();  
    s1.p();  
  }  
}  

在上述程序中,我可以用什么代替该关键字?

1 个答案:

答案 0 :(得分:0)

这已经习惯于引用当前类的实例变量。

class S2{  
  void m(S2 obj){  
    System.out.println("method is invoked");  
  }  
  void p(S2 obj){  
    m(obj); //instead of this keyword you can pass a reference and use, bad practice but just to help you for your understanding.

  }  
  public static void main(String args[]){  
    S2 s2 = new S2();  
    s2.p(s2);  
  }  
}