简体   繁体   English

不同类型集合的集合重载

[英]Collection overloading for different types of collections

I know that overloading is something which is decided at compile time but when i try to run below example its gives me result which i am not able to understand 我知道重载是在编译时决定的,但是当我尝试在下面的示例中运行时,它给了我无法理解的结果

package miscellaneous;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CollectionsOverloading {

public static String classify(Set<?> s) {
    return "Set";
}

public static String classify(List<?> s) {
    return "List";
}

public static String classify(Collection<?> s) {
    return "Collection";
}

public static void main (String args[]) {

    Collection<?>[] collections = { new HashSet<String>(), new ArrayList<String>(), new HashMap<String, String>().values()};

    for (Collection<?> coll : collections) {
        System.out.println(classify(coll));
    }
    }
}

When i run this code snippet every-time i get the output as "Collection" which means that the classify method with argument as Collection is called. 每次运行此代码段时,我的输出均为“ Collection”,这意味着将调用参数为Collection的分类方法。

Please explain 请解释

Since the classify method you are calling is static, you are choosing which one to call at compile-time, and not at run-time. 由于您要调用的classify方法是静态的,因此您可以选择在编译时而不是在运行时调用哪个方法。

At compile-time, the compiler sees that collections is an array of Collection , and therefore binds to the public static String classify(Collection<?> s) version of classify . 在编译时,编译器看到该collections是阵列Collection ,并因此结合到public static String classify(Collection<?> s)的版本classify

Edit : even if these methods were non-static, you would still find the Collection version being called, since overloaded methods are bonded using static binding at compile-time while overridden methods are bonded using dynamic binding at runtime . 编辑 :即使这些方法是非静态的,您仍然会发现正在调用Collection版本,因为重载的方法是在编译时使用静态绑定来绑定的,而重写的方法是在运行时使用动态绑定来绑定的

As you already said, the linking to overloaded Methods is made at compile time. 正如您已经说过的那样,链接到重载的方法是在编译时进行的。 As you iterate through a List of Collection the compiler only knows that the current element is an instance of Collection so it linkes to the classify(Collection) Method, which is then always called. 当您遍历Collection的列表时,编译器仅知道当前元素是Collection一个实例,因此它链接到classify(Collection)方法,然后始终调用该方法。

becaluse coll is the type of Collection ,so every time call classify(Collection s) methed.if you want call other methed,you need convert the type.Here is the code: 因为becaluse coll是Collection的类型,所以每次调用classify(Collection)方法。如果要调用其他方法,则需要转换类型。以下是代码:

Collection<?>[] collections = { new HashSet<String>(),new ArrayList<String>(), new HashMap<String, String>().values() };
for (Collection<?> coll : collections) {
    if(coll instanceof Set<?>){
        System.out.println(classify((Set<?>)coll));
    }
    else if(coll instanceof List<?>) {
        System.out.println(classify((List<?>)coll));
    }
    else {
        System.out.println(classify((Collection<?>)coll));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM