书上说:
you can also use the this reference as an argument to a method, in which an object passes a reference of itself to another object.翻译过来大概是:
你也可以把this引用作为方法的实参,把一个对象自己的引用传给另一个对象。请问这是怎么一种情况呢?这句抽象的话该怎样理解呢?哪位大侠可以给个实例么?

解决方案 »

  1.   

    汗上文在下面,大意是说在类内部,当域和方法被访问或调用时,Java会自动在域或方法前面加this,下文没有了,章节末尾
    Classroom Q & A
    Q: The this reference seems a little confusing. Is it necessary?
    A: The this reference is one of those fundamental concepts of OOP
    that tends to be confusing the first time you see it. To explain the
    need for the this reference, I always try to emphasize the fact that
    a field or method cannot be accessed without a reference. The this
    reference is the only way a method in a class can access the other
    fields or methods of the class.
    Q: Why does the compiler add this. automatically?
    A: It is strictly for convenience. It would be fairly tedious (although
    certainly feasible) to add this. every time it was required in a class.
    In a large class, there could easily be hundreds of accesses to the
    fields or methods of the class, each requiring the this reference.
    For now, keep in mind that the this reference is implicitly being
    used when a method in your class accesses a field of the class.
      

  2.   

    public class T

      public static void main(String args[])
      {
        A a = new A();
        B b = new B(a);
        a.fun();
      }
    }class A
    {
      B b;
      public A()
      {
      }  public void setB(B b)
      {
        this.b = b;
      }  public void fun()
      {
        System.out.println(b.i);
      }
    }class B
    {
      int i=3;
      public B(A a)
      {
        a.setB(this);
      }
    }
      

  3.   


    public class A { public static void main(String[] args) { new B().method(this); }}class B {
    public void method(A a) { }
    }
      

  4.   

    public class A {    public static void main(String[] args) {        new B().method(this);//调用B类的构造方法时将A对象本身作为形参传递过去    }}class B {
        public void method(A a) {    }
    }
      

  5.   

    this 
    当前对象的引用啊,引用当作参数