java中,怎么让两个对象互相持有对方的引用
1个回答
展开全部
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.");
}
}
{
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.");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询