final ByteArrayOutputStream os = new ByteArrayOutputStream();
/*
* 这里一下看不懂,红色字体的东西我都看不明白,谢谢各位乐
*
*
* 这里不理解下面的这个对象为什么是这样写的,
* 其中定义的write(byte[] data, int offset, int length) 方法的参数是从哪里来的?
* ServletOutPutStream 对象为什么要这样声明?有什么作用吗?
* 这样写的目的到底是什么?
*/
final ServletOutputStream stream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
os.write(data, offset, length);
}
public void write(int b) throws IOException {
os.write(b);
}
}; final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return stream;
}
public PrintWriter getWriter() {
return pw;
}
};
/*
* 这里以上看不懂
*/
/*
* 这里一下看不懂,红色字体的东西我都看不明白,谢谢各位乐
*
*
* 这里不理解下面的这个对象为什么是这样写的,
* 其中定义的write(byte[] data, int offset, int length) 方法的参数是从哪里来的?
* ServletOutPutStream 对象为什么要这样声明?有什么作用吗?
* 这样写的目的到底是什么?
*/
final ServletOutputStream stream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
os.write(data, offset, length);
}
public void write(int b) throws IOException {
os.write(b);
}
}; final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return stream;
}
public PrintWriter getWriter() {
return pw;
}
};
/*
* 这里以上看不懂
*/
解决方案 »
- Error creating bean with name 'entityManagerFactoryBean'
- 普通的java工程 ,不是web工程,连接mysql出错,高手请进
- 如何提高在国外网站的下载速度
- 请各位大虾请教,来帮帮小弟啊!
- 实现两个下拉类表移过来移过去的js代码
- 在使用struts时,由于继承了LookupDispatchAction,所以为其写了资源文件 出现异常
- xml Element节点 如何进行比较(java 语言)
- spring 异常 找了好久没找到
- 部署EJB出错......(再次发帖,在线等待)
- 是喜是悲?
- String s="asdajshduywueouqiwheuiqhwe" 写一个方法,求出出现次数最多的字母!大家帮哈忙啊!
- %%%% Error Creating SessionFactory %%%%
对象这样写就是匿名内部类,至于这个问题····“其中定义的write(byte[] data, int offset, int length) 方法的参数是从哪里来的? ” 为啥要问参数是从哪里来的?你到底问的是啥意思?这里的参数就是定义的参数啊,一般都是覆盖的父类的方法,参数并没有实例化,不调用就不知道哪里来的啊,反正我不知道你问的是啥意思,嘿嘿········
public class E11_ConnectionManager {
public static void main(String args[]) {
Connection c =
ConnectionManager.getConnection();
while(c != null) {
System.out.println(c);
c.doSomething();
c = ConnectionManager.getConnection();
}
}
}
package c05.connection;
public class Connection {
private static int counter = 0;
private int id = counter++;
Connection() {}
public String toString() {
return "Connection " + id;
}
public void doSomething() {}
} package c05.connection;
public class ConnectionManager {
private static Connection[] pool =
new Connection[10];
private static int counter = 0;
static {
for(int i = 0; i < pool.length; i++)
pool[i] = new Connection();
}
public static Connection getConnection() {
if(counter < pool.length)
return pool[counter++];
return null;
}
}
输出是:
Connection 0
Connection 1
Connection 2
Connection 3
Connection 4
Connection 5
Connection 6
Connection 7
Connection 8
Connection 9
我的问题就是:这个程序根本就没有调用Connection类中的toString()方法啊。而如果注释掉toString()这个方法,输出是:
com.proter.c.Connection@1fb8ee3
com.proter.c.Connection@61de33
com.proter.c.Connection@14318bb
com.proter.c.Connection@ca0b6
com.proter.c.Connection@10b30a7
com.proter.c.Connection@1a758cb
com.proter.c.Connection@1b67f74
com.proter.c.Connection@69b332
com.proter.c.Connection@173a10f
com.proter.c.Connection@530daa
我觉得呢,就算有toString()这个方法,也应是第二种输出啊,因为程序没有调用toString()方法。
诚请高手指教指教!!小弟在这谢过了!!
第一种是因为你的Connection中有toString()方法
第二种是因为你的Connnection中去了toString()方法
final ByteArrayOutputStream os = new ByteArrayOutputStream();
你是调用了连接后,把Connection方法中的所有属性及方法都传过去了