Java: Class.this

前端 未结 5 1175
野趣味
野趣味 2020-11-27 11:41

I have a Java program that looks like this.

public class LocalScreen {

   public void onMake() {
       aFuncCall(LocalScreen.this, oneString, twoString);
          


        
5条回答
  •  情深已故
    2020-11-27 12:11

    I know what is your confusion.I am encounter the problem just now, it should have special scene to distinguish them.

    class THIS {
      def andthen = {
        new THIS {
          println(THIS.this.## + ":inner-THIS.this.##")
          println(this.## + ":inner-this.##")
          new THIS {
            println(THIS.this.## + ":inner-inner-THIS.this.##")
            println(this.## + ":inner-this.##")
          }
        }
      }
      def getInfo = {
        println(THIS.this.## + ":THIS.this.##")
        println(this.## + ":this.##")
      }
    }
    

    You can see the diff between THIS.this and this in new THIS operation by hashcode( .## )

    test in scala console :

    scala> val x = new THIS
    x: THIS = THIS@5ab9b447
    
    scala> val y = x.andthen
    1522119751:inner-THIS.this.##
    404586280:inner-this.##
    1522119751:inner-inner-THIS.this.##
    2027227708:inner-this.##
    y: THIS = THIS$$anon$1@181d7f28
    
    scala> x.getInfo
    1522119751:THIS.this.##
    1522119751:this.##
    

    THIS.this always point to outer THIS class which is refer by val x,but this is beyond to anonymous new operation.

提交回复
热议问题