关于方法的泛型参数,知道的进来讨论一下,看看有没有这个功能 本帖最后由 psljkljfslc 于 2009-12-06 01:24:39 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以的,比如:public class Test { public static void main(String[] args) throws Exception { Test t = new Test(); byte[] bytes = "ABCD".getBytes(); t.foo(new ByteArrayInputStream(bytes)); t.foo(new BufferedInputStream(new ByteArrayInputStream(bytes))); t.foo(new DataInputStream(new ByteArrayInputStream(bytes))); } public <T extends InputStream> void foo(T input) throws Exception { byte[] buff = new byte[4096]; int len; while ((len = input.read(buff)) != -1) { System.out.println(new String(buff, 0, len)); } }}但是,就你的例子而言,只要用接口InputStream作为参数就可以了。 multiple makers 请问如何激活一个窗口(基于JFrame)? 如何动态设置jtable中某一个单元格的字体颜色 用java做界面好吗? list里面取随机几个对象问题 如何将焦点从internalframe中转移出来? 问一些关于junit的问题 如果将2130706433 与127.0.0.1两者进行互换 请问如何分辨java建立的是tcp还是udp,这段程序是tcp的客户端吧? “[Microsoft][ODBC 驱动程序 管理器] 无效的光标状态”是什么错误?? 如何理解面向对象? 怎么导入默认包的类?
public class Test { public static void main(String[] args) throws Exception {
Test t = new Test();
byte[] bytes = "ABCD".getBytes();
t.foo(new ByteArrayInputStream(bytes));
t.foo(new BufferedInputStream(new ByteArrayInputStream(bytes)));
t.foo(new DataInputStream(new ByteArrayInputStream(bytes)));
} public <T extends InputStream> void foo(T input) throws Exception {
byte[] buff = new byte[4096];
int len;
while ((len = input.read(buff)) != -1) {
System.out.println(new String(buff, 0, len));
}
}
}但是,就你的例子而言,只要用接口InputStream作为参数就可以了。