简体   繁体   English

任务02:此Java代码如何工作?

[英]Task 02: How does this Java Code work?

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.
    }
}

Okay guys, as seen above I have got a new Java Code, I think all in all I did understand many things, at least I did comments above the stuff I understood and thought my way of thinking would be correct. 好的,就像上面看到的,我有了一个新的Java代码,我认为我确实理解了很多事情,至少我对我所理解的东西做过评论,并认为我的思维方式是正确的。

Feel free and check my above comments and correct me if I am wrong. 请放心并检查我的上述评论,如果我错了,请纠正我。

I have tested and the above code actually prints following: 我已经测试过,上面的代码实际打印如下:

test: 03
t1: 03
test: 07
t2: 87

So in other words I was partially right, I simply don't understand why it is printing test: 07, since there is no loop, and why t2: 87 is not t2: 86? 因此,换句话说,我是部分正确的人,我根本不理解为什么打印测试:07,因为没有循环,为什么t2:87不是t2:86?

I was initially expecting something like that: 我最初期望的是这样的:

test: 03
t1: 03
t2: 86

Any profis wanting to check it? 任何专业人士想检查吗?

Thanks in advance. 提前致谢。

Learning is called every time you create a new Learning() , because it's the constructor for all instances of the Learning class. Learning被称为每次创建时new Learning()因为它是学习类的所有实例的构造函数。 You create two instances of Learning , therefore test gets printed twice. 您创建了Learning两个实例,因此test被打印两次。 The reason the value is 87 is because s is static . 值为87的原因是因为sstatic This means all instances of Learning share the same value for s . 这意味着Learning所有实例都共享s的相同值。 Therefore modifying t1 's instance of s to be 6 also modifies t2's instance of s , which then gets incremented in its constructor, and becomes 7 . 因此修改t1的实例s6还修改的T2的实例s ,然后把它在其构造递增,变得7

It's printing test: 07 because you are instantiating a new copy of Learning() as t2. 它正在打印test: 07因为您要实例化t2的Learning()的新副本。 Therefore, it's going in the constructor and printing the line in there. 因此,它将进入构造函数并在其中打印行。

Because it's being instantiated, this also increases the value of s in the constructor. 因为它被实例化,所以这也增加了构造函数中s的值。 Because s is a static variable, its value is shared among all Learning objects. 由于s是静态变量,因此其值在所有Learning对象之间共享。 Because you had set it to 6 in t1, it increases to 7 when you instantiate the new object, yielding '87'. 因为您在t1中将其设置为6,所以在实例化新对象时将其增加到7,从而产生'87'。

Since you create 2 objects of the class learning, the "test: 0x" will print twice. 由于您创建了学习类的2个对象,因此“ test:0x”将打印两次。

The reason why t2 is 86 and not 87 is because you first set t1.s = 6 and, after that, call the constrctor new Learning() , which will increment s by 1. t2是86而不是87的原因是因为首先设置t1.s = 6 ,然后调用t1.s = 6 new Learning() ,它将s加1。

Look at this line: Learning t2 = new Learning(); 看这行: Learning t2 = new Learning();

This will cause test: 07 to be printed out because it uses the Learning() constructor which has a print statement in it. 这将导致test: 07被打印出来,因为它使用了带有打印语句的Learning()构造函数。

This line also causes s to increase by 1 from 6 to 7. This is why t2:87 is printed instead of t2:86 此行还使s从6到7增加1。这就是为什么打印t2:87而不是t2:86

Every time you create a new instance of Learning type, the constructor will be invoked. 每次创建Learning类型的新实例时,都会调用构造函数。 So when you create the t2 object the s value is incremented by one(that's why you have the 87 value printed instead of 86 and the text test: 07 is printed. 因此,当您创建t2对象时, s值将增加1(这就是为什么您将87值而不是86值打印出来并打印文本test: 07原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM