引用问题

 

package jdk.ref;



/**

 * @author <a href="mailto:[email protected]">张挺</a>

 * @since 2010-4-27 10:01:45

 */

public class RefDemo {

    public static void main(String[] args) {

        A a = new A();

        System.out.println(a.b.getA());

        B b = a.getB(); //获得了引用

        b.setA(3);

        System.out.println(a.b.getA());

        b = new B(); //完全改变了引用的对象

        b.setA(5);

        System.out.println(a.b.getA());



    }

}



class A {

    B b = new B();



    public B getB() {

        return b;

    }



    public void setB(B b) {

        this.b = b;

    }

}



class B {

    int a =2;



    void setA(int a) {

        this.a = a;

    }



    int getA() {

        return this.a;

    }

}

 

 

简单放个引用问题
output:
2
3
3

你可能感兴趣的:(问题)