Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
任务02:这个Java代码是如何工作的?_Java - Fatal编程技术网

任务02:这个Java代码是如何工作的?

任务02:这个Java代码是如何工作的?,java,Java,好的,伙计们,正如上面所看到的,我已经得到了一个新的Java代码,我想总的来说我确实理解了很多东西,至少我对我理解的东西做了评论,并且认为我的思维方式是正确的 请随时检查我的上述评论,如果我错了,请纠正我 我已经测试过,上面的代码实际上打印了以下内容: public class Learning { int i = 1; // i is assigned to 1. static int s = 2;// s is assigned to 2. Learning() {

好的,伙计们,正如上面所看到的,我已经得到了一个新的Java代码,我想总的来说我确实理解了很多东西,至少我对我理解的东西做了评论,并且认为我的思维方式是正确的

请随时检查我的上述评论,如果我错了,请纠正我

我已经测试过,上面的代码实际上打印了以下内容:

public class Learning {
    int i = 1; // i is assigned to 1.
    static int s = 2;// s is assigned to 2.

    Learning() {
        i = 0; s++; // i is assigned to 0 and s increases to 2. 
        System.out.println("test: " + i + s);// this line will display test: 03 since the values are above given.
    }

    public static void main (String [] args) {
        Learning t1 = new Learning();// Creating and instance variable from the Class Learning.
        System.out.println("t1: " + t1.i + t1.s);//t1 reaches with the help of the . the value of i as well as s and again this line will print out t1: 03
        t1.s = 6;// Now the variable get s new value which is 6
        Learning t2 = new Learning();// Creating another instance variable from the class called Learning
        t2.i = 8;// Now the value for i is set for 8.
        System.out.println("t2: " + t2.i + t2.s);// Now I thought the program would display t2: 86 since I have already got the value 8 and 6 assigned above, but it doesn't do that.
    }
}
换句话说,我有一部分是对的,我只是不明白为什么打印test:07,因为没有循环,为什么t2:87不是t2:86

我最初期望的是这样的:

test: 03
t1: 03
test: 07
t2: 87
有人想检查一下吗


提前感谢。

它正在打印
test:07
,因为您正在将
Learning()
的新副本实例化为t2。因此,它进入构造函数并在其中打印行


因为它正在被实例化,这也会增加构造函数中
s
的值。因为
s
是一个静态变量,它的值在所有
学习
对象之间共享。因为您在t1中将它设置为6,所以当您实例化新对象时,它会增加到7,产生“87”。

它正在打印
test:07
,因为您正在将
Learning()
的新副本实例化为t2。因此,它进入构造函数并在其中打印行


因为它正在被实例化,这也会增加构造函数中
s
的值。因为
s
是一个静态变量,它的值在所有
学习
对象之间共享。因为您在t1中将其设置为6,所以当您实例化新对象时,它会增加到7,产生“87”。

每次创建
新学习()
时,都会调用
学习,因为它是学习类的所有实例的构造函数。您创建了两个
学习
实例,因此
测试
将打印两次。该值为
87
的原因是
s
静态的
。这意味着
Learning
的所有实例对
s
共享相同的值。因此,将
t1
s
实例修改为
6
也会修改t2的
s
实例,该实例随后在其构造函数中递增,并变为
7

每次创建
新学习()时都会调用
学习
,因为它是学习类的所有实例的构造函数。您创建了两个
学习
实例,因此
测试
将打印两次。该值为
87
的原因是
s
静态的
。这意味着
Learning
的所有实例对
s
共享相同的值。因此,将
t1
s
实例修改为
6
也会修改t2的
s
实例,然后该实例在其构造函数中递增,变成
7

,因为您创建了两个类学习对象,“test:0x”将打印两次


t2是86而不是87的原因是您首先设置了
t1.s=6
,然后调用构造函数
new Learning()
,它将
s
增加1。

因为您创建了两个类学习对象,“test:0x”将打印两次


t2是86而不是87的原因是您首先设置了
t1.s=6
,然后调用构造函数
new Learning()
,它将
s
增加1。

看这一行:
Learning t2=new Learning()

这将导致打印出
test:07
,因为它使用了带有print语句的Learning()构造函数


这条线还会使s从6增加到7,增加1。这就是为什么打印的是
t2:87
而不是
t2:86

请看这行:
Learning t2=new Learning()

这将导致打印出
test:07
,因为它使用了带有print语句的Learning()构造函数


这条线还会使s从6增加到7,增加1。这就是为什么每次创建
Learning
类型的新实例时,都会打印
t2:87
而不是
t2:86

。因此,当您创建
t2
对象时,
s
值增加1(这就是为什么要打印
87
值而不是
86
,并且打印文本
test:07

每次创建
Learning
类型的新实例时,都会调用构造函数。因此,当您创建
t2
对象时,
s
值会增加1(这就是为什么您打印了
87
值而不是
86
,并且打印了文本
test:07

我建议您研究静态变量和成员变量之间的差异。是时候学习使用调试器了。@code Guru,我通过示例学习得最好。我希望会有一些知识渊博且体面的用户提供帮助。)让我看看这段代码。@user3330060如果你搜索“Java静态变量”,我相信你会找到一些例子。此外,Java教程中有很多很好的例子。我建议你研究静态变量和成员变量之间的区别。是时候学习使用调试器了。@Code Guru,我通过例子学习得最好。我希望会有一些知识渊博的正派用户帮助我完成这段代码。@user3330060如果你在googl“Java静态变量”,我相信你会找到一些例子。而且,Java教程中有很多很好的例子。
test: 03
t1: 03
t2: 86