java泛型边界类型 [英] java generics bounds type

查看:121
本文介绍了java泛型边界类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下两个签名是否相同?



  public static< T> void work(Class< T> type,T instance); 

  public static< T,S extends T> void work(Class< T> type,S instance); 


解决方案

不,这两个签名是不一样的。从 Java语言规范,第8章
$ b


如果两个方法具有相同的名称和参数类型,则它们具有相同的签名。



如果满足以下所有条件,则两个方法或构造函数声明M和N具有相同的参数类型:


  • 它们具有相同数量的形式参数(可能为零)

  • 它们具有相同数量的类型参数(可能为零)









  • $因为你的两种方法不能共享相同数量的类型参数,签名不相同。


    在实际使用隐式类型参数调用方法的情况下,它们可能会被视为可以互换。但这只是在源头层面,从来不在二元层面。换句话说,如果您在类 Foo 中具有 work()的单类型参数版本,并且它是( Bar )中的方法调用,然后切换到 work()的两类型参数版本,并重新编译 Foo ,您还需要重新编译 Bar



    编辑



    @onepotato询问:


    如果他们不具有相同的签名,那么为什么当我将它们复制并粘贴到一个类中时,Eclipse告诉我它们具有相同的方法签名?

    两个签名相等和两个签名冲突(即覆盖等效)之间存在差异。如果其中一个是另一个的子签名,则两个签名会发生冲突。这将在后面的相同部分中进行解释:


    两个方法签名m1和m2是覆盖等效的,如果m1是m2的子签名或m2是m1的子签名。



    在类中声明两个具有重写等价签名的方法是编译时错误。



    方法m1的签名是方法m2的签名的子签名,如果:b


    • m2与m1相同的签名,或者m1的签名与m2的签名的删除(第4.6节)相同。



    Are following two signatures same?

    public static <T> void work(Class<T> type, T instance);
    

    and

    public static <T, S extends T> void work(Class<T> type, S instance);
    

    解决方案

    No, the two signatures are not the same. From the Java Language Spec, Chapter 8:

    Two methods have the same signature if they have the same name and argument types.

    Two method or constructor declarations M and N have the same argument types if all of the following conditions hold:

    • They have the same number of formal parameters (possibly zero)
    • They have the same number of type parameters (possibly zero)

    ...

    Since your two methods do not share the same number of type parameters, the signatures are not the same.

    In practical cases where the methods are called with implicit type parameters, they might be seen as interchangeable. But this is only at the source level, never at the binary level. In other words, if you had the one-type-parameter version of work() in class Foo and it was called by a method in class Bar, and then you switched to the two-type-parameter version of work() and recompiled Foo, you would also need to recompile Bar.

    Edit

    @onepotato asks:

    If they don't have the same signatures, then why when I copy and paste them in one class Eclipse tell me that they have the same method signature?

    There is a difference between two signatures being equal and two signatures conflicting (being "override-equivalent"). Two signatures conflict if one is a subsignature of the other. This is explained later in the same section:

    Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.

    It is a compile-time error to declare two methods with override-equivalent signatures in a class.

    The signature of a method m1 is a subsignature of the signature of a method m2 if either:

    • m2 has the same signature as m1, or
    • the signature of m1 is the same as the erasure (§4.6) of the signature of m2.

    这篇关于java泛型边界类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆