1 import java.io.PrintStream; 2 3 /* 4 * 5 * public class ValueTransferTest4 6 *{ 7 * public static void main(String[] args) 8 * { 9 * int a =10; 10 * int b =10; 11 12 method(a,b); // 在不改变原题情况下,调用method 输出a=100 b = 100 13 14 System.out.println(a); 15 System.out.println(b); 16 17 } 18 * 19 */ 20 public class ValueTransferTest4 21 { 22 public static void main(String[] args) 23 { 24 int a =10; 25 int b =10; 26 27 method(a,b); // 28 29 System.out.println("a = "+a); 30 System.out.println("b = "+b); 31 32 } 33 34 //方式一 35 // public static void method(int a,int b) 36 // { 37 // a = a*10; 38 // b = b*10; 39 // System.out.println(a); 40 // System.out.println(b); 41 // System.exit(0); 42 // 43 // 44 // } 45 46 //方式二 47 public static void method(int a,int b) 48 { 49 PrintStream ps = new PrintStream(System.out) 50 { 51 @Override 52 public void println(String x) 53 { 54 if("a = 10".equals(x)) 55 { 56 x ="a = 100"; 57 58 } 59 else if("b = 10".equals(x)) 60 { 61 x = "b = 200"; 62 } 63 super.println(x); 64 65 } 66 67 }; 68 System.setOut(ps); 69 70 71 } 72