日期:2014-05-20 浏览次数:21200 次
package com.xiuxiu.table;
public class A {
private B b;
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public static void main(String[] args) {
A a = new A();
B b = new B();
a.setB(b);
b.setA(a);
}
}
class B {
private A a;
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
}
class A{
private B b;
public A(B b) {
this.b = b;
}
}
class Producer
{
private Consumer c = null;
Producer(Consumer c)
{
this.c = c;
c.firstMethod(); //持有对方引用
}
public void secondMethod()
{
System.out.println("Producer.");
}
}
class Consumer
{
private Producer p = null;
Consumer(Producer p)
{
this.p = p;
p.secondMethod(); //持有对方引用
}
public void firstMethod()
{
System.out.println("Consumer.");
}
}