java中的循环引用 [英] Cyclic reference in java

查看:1702
本文介绍了java中的循环引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果A类引用了B类而B类引用了A类,那么它在Java中是否可以接受?如果是,当我们执行A类时,B类将不会被实例化。那么编译器将如何执行B类的引用?

If class A has a reference of class B and class B has a reference of class A, is it acceptable in Java? If yes, When we execute class A by the time class B will not be instantiated. So how the compiler will execute the reference of class B?

TIA

推荐答案


如果A类引用了B类而B类引用了A类,那么它在Java中是否可以接受?

If class A has a reference of class B and class B has a reference of class A, is it acceptable in Java?

是。


当我们执行时时间类B的A类不会被实例化。那么编译器将如何执行B类的引用?

When we execute class A by the time class B will not be instantiated. So how the compiler will execute the reference of class B?

你必须先创建一个对象来创建循环引用,然后再另一个,然后关闭循环;例如这样的事情。

You have to create the cyclic reference by creating one object first, and then the other, and then closing the cycle; e.g. something like this.

A a = new A();
B b = new B(a);
a.setB(b);

(这假设 A 类有已经用 setB(A)方法定义。还有其他形成循环的方法,但这是最简单的。)

(This assumes that the A class has been defined with a setB(A) method. There are other ways to form the cycle too, but this is the simplest.)

但请注意,你的术语都是完全错误的。

But note that your terminology is all completely wrong.


  • 你不要执行一个班级。您实例化创建类的实例。

  • 编译器(例如 javac )没有执行任何东西......它只是编译代码。

  • 执行的东西(即运行)Java代码是Java虚拟机;例如当你使用 java 命令。

  • You don't "execute" a class. You instantiate or create an instance of a class.
  • The compiler (for example javac) doesn't "execute" anything ... it just compiles code.
  • The thing that executes (i.e runs) Java code is a Java Virtual Machine; e.g. when you use the java command.

(你学习很重要如果要与其他IT专业人员进行通信,请使用正确的Java术语。它避免了很多错误传达和混淆!)

(It is important that you learn and use the correct Java terminology if you are going to communicate with other IT professionals. It avoids a lot of miscommunication and confusion!)

这篇关于java中的循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆