Talk about the way of calling in Java

xiaoxiao2021-03-06  26

Many books say that the Java supports the call to the call, similar to the Person & A reference calls in C , and recently programming a series of issues, which makes me have doubts, so I will list these methods one by one, let's come together Take a look at the call mode in Java: Look below: Class Person {Private String Name; // Name Private String Sex; // Gender Public Person (String X, String Y) {this.name = x; this.sex = Y;} public void setStatus (string x, string y) {this.name = x; this.sex = y;} public string toString () {return name sex;} // ----- Exchange normal object - ---- Public Static Void Changeref (Person Tmpx, Person TMPY) {// Exchange TMPX and TMPY Object Person swapRef = Tmpx; TMPX = TMPY; TMPY = swapRef; // system.out.println ("Exchange in method RESULTS: REFA = " TMPX.TOString ()); // system.out.println (" Results Exchange in Method: Refb = " TMPY.TOSTRING ());} // ----- Exchange array Object ----- Public Static Void ChangeArrayref (Person [] X, Person [] Y) {// Exchange array object Person swapArrayref = x [x.length-1]; x [x.ley-1] = y x.length-1]; y [x.length-1] = swapArrayref;} // ----- Exchange array ----- public Static void changeArray (int [] x, int [] y) {int [] TMP = x; x = y; y = tmp;}} public class demo {public static void main (string [] args) {// ------ Establish and construct two objects --------- Person Refa = New Person ("Zhang San"

, "Men"); Person Refb = New Person ("Li Si", "Men"); // Exchange the REFA object and the REFB object Person.ChangeRef (REFA, REFB); // As seen from the exchange result, actual object The SYSTEM.OUT.PRINTLN was not exchanged ("Results in the main function REFA =" refa.tostring ()); System.out.Println ("Results in the main function REFB =" refb.toString ))); // ------- Create two object arrays ---------- Person [] arraya = new person [1]; person [] arrayb = new person [1]; / / Structure Array Object Arraya [0] = New Person ("Wang 5", "Men"); Arrayb [0] = New Person ("Zhao Sa", "Men"); / * When the array object is NULL, it cannot Set its value, you must construct it first (that is, call the constructor), then set its value with other methods * / system.out.Println ('/ n' "array object exchange before exchange of arraya =" arraya [0] .tostring ()); system.out.println ("ARRAYB =" before "Array Object Exchange Arrayb [0] .tostring ()); // Exchange these two array objects Person.changeArrayref (Arraya, Arrayb); System.out.println ("- ARRAYA =" Arraya [0]. Tostring ()); system.out.println ("- ARRAYB =" arrayb [0] .tostring () ); // ------- Create two ordinary arrays -------- int [] a = New int [2]; int [] b = new int [2]; // assign a value for array of elements for (int i = 0; I 转载请注明原文地址:https://www.9cbs.com/read-39467.html


New Post(0)