String.this什么意思?".this"作用是什么?

解决方案 »

  1.   

    java.lang.String 中没有这样的代码。String.this.xxxx 表示在 String 的非静态内部类中使用 String 中的方法或者属性。比如:String 中有个方法叫 intern(),但我在 String 的一个内部类中也有个方法叫 intern(),如果我在这个内部类中直接使用 intern() 的话是调用自己类中的那个方法。如果想调用外部 String 类中的那个方法就可以使用 String.this.intern(); 进行调用。由于是非静态内部类,因此可以使用 this,静态内部类的话是不能这样用的。
      

  2.   

    import static java.lang.String.valueOf;
      

  3.   

    肯定是跟内部类有关的,给个例子如下;class A {
       BBB bbb=new BBB(){
            public void method1(){
                this.test();  //将打印  BBB.test is called.
                A.this.test();  //将打印 A.test is called
            }
            public void test(){
              System.out.println("BBB.test is called");
            }
        }     public void test(){
              System.out.println("A.test is called");
          }
    }interface BBB{
       void method1();
       void test();
    }这样说,楼主应该很清楚了吧。