public class Swap2 { public static void Swap2 (IDemo a, IDemo b) { int temp = a.i; a.i = b.i; b.i = temp; } public static void main (String args []) { IDemo a, b; a = new IDemo ( 10 ); b = new IDemo ( 20 ); System.out.println ( "a is " + a); System.out.println ( "b is " + b); Swap2.Swap2 (a, b); System.out.println ( "a is " + a); System.out.println ( "b is " + b); } } class IDemo { protected int i; public IDemo(int _i) { i=_i; } public String toString() { return String.valueOf(i); } }
---------- java ---------- a is 10 b is 20 a is 20 b is 10 Output completed (1 sec consumed) - Normal Termination
时间: 2024-10-22 22:35:51