gpt4 book ai didi

java - 我不明白这个 Java 示例中的代码行 "temp.next = this;"

转载 作者:行者123 更新时间:2023-12-01 21:09:30 25 4
gpt4 key购买 nike

我试图理解 Java 中关键字“this”的用法,但最终无法理解以下示例中的第 17、18、22 和 25 行:

1.public class LinkMeUp
2.{
3. private int data;
4. private LinkMeUp next;
5.
6. LinkMeUp(int num)
7. {
8. data = num * num; next = null;
9. }
10. LinkMeUp()
11. {
12. this(0);
13. }
14. LinkMeUp add(int num)
15. {
16. LinkMeUp temp = new LinkMeUp(num);
17. temp.next = this;
18. return(temp);
19. }
20. void print()
21. {
22. LinkMeUp temp = this;
23. while(temp != null)
24. {
25. System.out.println(temp.data);
26. temp = temp.next;
27. }
28. }
29. public static void main(String[] args)
30. {
31. LinkMeUp link = new LinkMeUp();
32. for(int k =1; k < 10; k++)
33. link = link.add(k);
34. link.print();
35. }
36.}

据我了解,在主方法中,创建了 LinkMeUp 对象并为其分配了要链接的内存地址。然后它将转到默认构造函数 LinkMeUp()。

默认构造函数内的代码行 this(0) 将调用另一个构造函数 LinkMeUp(int num) 并设置 data = 0 * 0 = 0 和 next = null。

然后返回到 main 方法进入循环,将第一个 k = 1 传递给 add(int num) 方法。在 add(int num) 方法中,它创建另一个 LinkMeUp 对象并分配给引用 temp。

我不明白 temp.next = this; “this”是指 LinkMeUp 类还是 LinkMeUp(int num)?我不理解 temp.next,因为“next”不是辅助方法,而是对 LinkMeUp 对象的另一个引用,该对象在 LinkMeUp(int num) 构造函数中分配为 null。

我也无法理解第 22 行和第 25 行

顺便说一句,这是程序的输出

81
64
49
36
25
16
9
4
1
0

最佳答案

I don't understand the temp.next = this; Is "this" referred to LinkMeUp class or LinkMeUp(int num)?

都不是。该行位于 add方法,所以this是对当前 LinkMeUp 的引用实例,而不是构造函数。 (即使在构造函数中,由于它没有被调用 - 例如 this() - 它不会调用构造函数。) LinkMeUp.add创建 LinkMeUp 的新实例通过LinkMeUp(num) ,设置其 next成员(member)至this ,并返回它。所以现在当前实例(add 被调用)是 next在新实例上add创建,并返回。

main中的循环中,我们看到实例 add创建被保留:

link = link.add(k);

这就是链的创建方式。

让我们遵循逻辑:

1. main创建 LinkMeUp 的实例使用无参数构造函数,因此我们有一个实例,其 data0 :

           +−−−−−−−−−−−−+link−−−−−−>|  LinkMeUp  |           +−−−−−−−−−−−−+           | data: 0    |           | next: null |           +−−−−−−−−−−−−+

2. Then we go into the loop with k == 1 and call link = link.add(k);. add calls new LinkMeUp(1) which creates a new instance and sets its data to 1, sets that new instance's next to the current instance, and returns it; the link = link.add(k); line updates link, and so after all that we have:

           +−−−−−−−−−−−−+link−−−−−−>|  LinkMeUp  |           +−−−−−−−−−−−−+           | data: 1    |     +−−−−−−−−−−−−+           | next       |−−−−>|  LinkMeUp  |           +−−−−−−−−−−−−+     +−−−−−−−−−−−−+                              | data: 0    |                              | next: null |                              +−−−−−−−−−−−−+

Note how the first instance (data === 0) is now at the end of the chain, and link refers to the new instance returned by add (data === 1).

3. Then the loop continues with k == 2 and calls link = link.add(k); again, which creates and returns a new instance with data == 4 (2 * 2). After that we have:

           +−−−−−−−−−−−−+          link−−−−−−>|  LinkMeUp  |                     +−−−−−−−−−−−−+                     | data: 4    |     +−−−−−−−−−−−−+                                   | next       |−−−−>|  LinkMeUp  |                                   +−−−−−−−−−−−−+     +−−−−−−−−−−−−+                                                      | data: 1    |     +−−−−−−−−−−−−+                                   | next       |−−−−>|  LinkMeUp  |                                   +−−−−−−−−−−−−+     +−−−−−−−−−−−−+                                                      | data: 0    |                                                 | next: null |                                                 +−−−−−−−−−−−−+

...and so on until after k == 9.

I also have trouble understanding line 22 and 25

Line 22:

LinkMeUp temp = this;

...设置变量temp到当前实例(当 add(k)k9 创建的实例)。然后,虽然该变量是 != null ,我们循环,输出temp.data 。所以第一个循环,它将输出 81 (当 data 时,通过 add(k) 创建的实例上的 k == 9 )。然后第 26 行:

temp = temp.next;

...将我们带到链中的下一个条目,这是 add(k) 创建的实例当k8 。我们输出它的data值( 64 )。然后我们继续循环,直到到达链的末尾,temp == null .

关于java - 我不明白这个 Java 示例中的代码行 "temp.next = this;",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41422597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com