1
vainly 2014-08-13 08:07:42 +08:00
public void process(object deals){
if (details instanceof Collection) { //process the type of Collection } if (details instanceof Array) { //process the type of Array } } |
2
Mutoo 2014-08-13 09:04:07 +08:00
|
3
cxe2v 2014-08-13 09:11:27 +08:00
我只晓得在C#里有个IEnumrable接口可以代表这两个类型
|
5
Mutoo 2014-08-13 17:43:25 +08:00
@andybest 数组本身就是 Iterable 的,你只要用
public void process(Iterable<Deal> deals) 就可以同时传 Collection<Deal> 和 Deal[] 了 |
6
andybest OP @Mutoo 不行吧?你测试下:
public static void process(Iterable<String> deals){ System.out.println(deals.toString()); } public static void main(String[] args) { String[] s1={"aaa","bbb","ccc"}; Collection<String> s2=new ArrayList<String>(); process(s1); process(s2); } |
7
Mutoo 2014-08-13 18:37:27 +08:00
|
8
SoloCompany 2014-08-13 23:48:40 +08:00
for : 是语法糖,Iterable 接口和数组生成的bytcode是不同的。。
|