简体   繁体   English

请帮助我理解以下代码中的“分配”方法

[英]Please help me understand the 'assign' method in the following code

class Test { 

    int a, b; 

    Test(int a, int b) {    
        this.a = a; 
        this.b = b; 
    } 

    Test() {               
        a = 0; 
        b = 0; 
    } 

    void print() { 
        System.out.println("A=" + a + ", B=" + b); 
    } 

    void assign(Test ob) {  //How can a class(Test) be passed as an argument in a method?
        this.a = ob.a;      //What does ob.a do?
        this.b = ob.b;      //What does ob.b do?
    }                       //How can a object 'ob' be passed as an argument?
} 

.

class TestDem { 

    public static void main(String ar[]) { 
        Test ob1 = new Test(1, 2); 
        System.out.println("1st object"); 
        ob1.print(); 
        Test ob2 = new Test(); 
        System.out.println("2nd object"); 
        ob2.print(); 
        ob2.assign(ob1); //dont understand how this statement works with the 'assign' method 
        System.out.println("After assigning object 1 to object 2"); 
        System.out.println("1st object"); 
        ob1.print(); 
        System.out.println("2nd object"); 
        ob2.print(); 

    } 
} 

Output 输出量

1st object
A=1, B=2
2nd object
A=0, B=0
After assigning object 1 to object 2
1st object
A=1, B=2
2nd object 
A=1, B=2
void assign(Test ob) {  
    this.a = ob.a;      
    this.b = ob.b;      //What does ob.b do?
}

This method is similar to the constructor you have used: - 此方法类似于您使用的构造函数:-

public Test(int a, int b) {
    this.a = a;
    this.b = b;
}
  • Difference is that, in this method, you are getting the values of a and b from the reference passed rather than the parameter a, and b itself.. 区别在于,在此方法中,您是从传递的引用而不是参数a和b本身中获取ab的值。
  • Also note that it is not a constructor.. It is just assigning the value of passed reference to the current object.. Whereas constructor initializes the instance's state after it is created.. 另请注意,它不是构造函数。它只是将传递的引用的值分配给当前对象。而构造函数在创建实例后初始化其状态。

How can a class(Test) be passed as an argument in a method? 如何在方法中将类(测试)作为参数传递?

We are not passing the class Test here, rather, we are passing a reference to an object of class Test.. 我们不是在这里传递 Test类 ,而是在传递 Test类对象的引用

This method is doing nothing but creating a copy of the object pointed by that reference (ob in this case).. So, after this method executes, the current object(pointed by this ) will have the same value as the passed object.. 该方法什么也不做,只是创建该引用所指向的对象的副本(在这种情况下为ob)。因此,执行此方法后,当前对象(由this指向)将具有与传递的对象相同的值。

What does ob.a do? ob.a是做什么的?

ob.a refers to the instance variable a for the reference ob of class Test , that you passed into this method.. ob.a引用传递给此方法的Test类的参考obinstance变量a

So, if you have: - 因此,如果您有:-

Test ob2 = new Test(1, 2);
Test ob1 = new Test();
ob1.assign(ob2);

Then when this method executes: - 然后在执行此方法时:-

Since, this refers to the current reference, so this in method assign will refer to ob1 , since we are calling on this reference.. 因为, this是指当前的参考,所以this在方法assign将参考ob1 ,因为我们在这个参考调用..

And, you will have both the reference ob2 in main method, and ob in the parameter of assign method pointing to the same object.. 并且,您将在main方法中同时具有引用ob2 ,在assign方法的参数中具有指向同一对象的引用ob

So, ob1.a(this.a) will be equal to ob2.a , and 因此, ob1.a(this.a)等于ob2.a ,并且
ob1.b(this.b) will be equal to ob2.b ob1.b(this.b)将等于ob2.b

void assign(Test ob) {  //How can a class(Test) be passed as an argument in a method?
    this.a = ob.a;      //What does ob.a do?
    this.b = ob.b;      //What does ob.b do?
}                       //How can a object 'ob' be passed as an argument?

In Java (apart from primitive types like int, etc. let's not talk about them here) everything extends Object. 在Java中(除了基本类型(如int等,我们在这里不再赘述)),所有内容都扩展了Object。

You defined a class Test that implictely extends Object . 您定义了一个Test类,它隐式扩展了Object This class has two member variables: a and b , which are of type int . 此类具有两个成员变量: ab ,它们的类型均为int

If ob is of type Test , you can access its members using ob.a and ob.b (as a and b are public). 如果obTest类型,则可以使用ob.aob.b (因为ab是公共的)访问其成员。 And you can set their value using ob.a = anIntValue and ob.b = anIntValue . 您可以使用ob.a = anIntValueob.b = anIntValue设置它们的值。

That is what you do in the assign method: you pass another object of type Test and copy its values into your enclosing object ( this.a = value , this.b = value ). 这是你在做什么assign方法:你通过类型的另一个对象Test和它的值复制到您的包围对象( this.a = valuethis.b = value )。

So the result of your method is to modify your internal state by copying the state of an external object. 因此,方法的结果是通过复制外部对象的状态来修改内部状态。

The method void assign(Test ob) takes an object ob of type Test . 方法void assign(Test ob)接受类型为Test的对象ob It then assigns the value of ob 's a value to the class that the method is being called on's a value, and then the same for the b values. 然后,它将ob的值a值分配给要在on的a值上调用该方法的类,然后将b值的值相同。

Perhaps if the method was renamed copy(Test ob) it would be clearer what the method does? 也许如果将方法重命名为copy(Test ob) ,它将更清楚该方法的作用?

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

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