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();
}
}
输出
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