我试图理解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)并将数据设置为0 * 0 = 0和next = null。

然后返回主方法进入循环,将第一个k = 1传递给add(int num)方法。在add(int num)方法内部,它创建另一个LinkMeUp对象,并分配给参考温度。

我不明白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

最佳答案

我不明白temp.next = this; “ this”是指LinkMeUp类还是LinkMeUp(int num)?


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

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

link = link.add(k);


这就是创建链的方式。

让我们遵循以下逻辑:

1. add使用no-parameters构造函数创建main的实例,因此我们有一个LinkMeUpdata的实例:

           +−−−−−−−−−−−−+
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.

Line 22:

LinkMeUp temp = this;


...将变量0设置为当前实例(当tempadd(k)时由k创建的变量)。然后,当变量为9时,我们循环输出!= null。因此,第一个循环将输出temp.data(在81时通过data创建的实例上的add(k))。然后第26行:

temp = temp.next;


...将我们带到链中的下一个条目,这是k == 9add(k)时由k创建的实例。我们输出其8值(data)。然后我们不断循环直到到达链的末尾64

10-06 05:48