赞
踩
同事今天贴出了一道最近网上某公司的笔试题,
public class HFTech { public static void main(String[] args) { int a = 10; int b = 20; method(a, b);//需要在method方法被调用之后,仅打印出a=100,b=200,请写出method方法的代码 System.out.println("a=" + a); System.out.println("b=" + b); }//代码编写处}
参考原文地址:
http://blog.csdn.net/rickiyeat/article/details/55049876
package com.xx.Interview;
import java.io.PrintStream; /** * Created by superdaojian on 17/2/16. */ public class HFTech { //方法1,直接在方法中打印字符串,然后提前退出 public static void method2(int a,int b) { System.out.print("a=100 b=200"); System.exit(0); } //方法2,切入新的输出流,覆盖流的方法 public static void method(final int a,final int b) { PrintStream stream = new PrintStream(System.out){ @Override public void print(String s) { super.print(s.replace(a+"", a*10+"").replace(b+"", b*10+"")); } }; System.setOut(stream); } public static void main(String[] args) { int a = 10; int b = 20; method(a, b); System.out.println("a=" + a); System.out.println("b=" + b); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。