Object reference issues in Java.

xiaoxiao2021-03-06  71

Test procedure: public class test {private string name; public string getname () {return name;} public void setname (String name) {this.name = name;

Public test () {}

Public Void Call (Test T1, Test T2) {System.out.Println ("T1 Address 1 =" T1); Test T3 = New Test (); T1.setName ("AAA"); t2.setname ("BBB "); T3.setname (" CCC "); System.out.Println (T1.GetName ()); // aaa system.out.println (t2.getname ()); // bbb t1 = t2; system. Out.println (t1.getname ()); // bbb T1 = T3; system.out.println (t1.getname ()); // ccc system.out.println (t3.getname ()); // CCC T3.setname ("eeee"); system.out.println ("T1 Address 2 =" T1);

Public static void main (string [] arg) {test t1 = new test (); test t2 = new test (); system.out.println ("T1 address 0 =" t1); test t4 = new test () T4.Call (T1, T2); System.out.Println ("T1 Address 3 =" T1); System.out.Println ("T1 =" T1.GetName ()); // AAA ??? System.out.println ("t2 =" t2.getname ()); // bbb ????} / understanding, before, it is very good, and now it is discovered. Re-understand again. When the object is used as a parameter, the incoming object is not an object, nor is an address of an object, but an reference to an object address (copying a address pointer). Therefore, when the object (assignment) = in the method of the object is parameter, the change is just the pointer of the address reference, and the address of the real object has not changed. I don't know if I understand it, first this way. It seems that I need to reread Java programming thoughts. Oh, recently understanding the ability to decline. The brain is sluggable. depressed.

转载请注明原文地址:https://www.9cbs.com/read-91049.html

New Post(0)