加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

为什么Java Type Erasure没有阻止编译此代码

发布时间:2020-12-15 04:25:26 所属栏目:Java 来源:网络整理
导读:我有一个类定义了以下两个方法: public MapString,MapString,String method(final MapString,String data)public boolean method(final MapString,String data) 基于Java类型擦除泛型,这段代码不应该编译,因为它们最终都是: method(Map data) 但是,此代码
我有一个类定义了以下两个方法:

public Map<String,Map<String,String>> method(final Map<String,String>> data)

public boolean method(final Map<String,String> data)

基于Java类型擦除泛型,这段代码不应该编译,因为它们最终都是:

method(Map data)

但是,此代码已在Java 6中成功编译,但未在Java 8中编译.

有人可以让我知道为什么它可以在Java 6下编译?

解决方法

它在Java 6下编译,但在Java 7或Java 8中编译.

Java 5和Java 6中有一个错误是fixed in Java 7 (#6182950).

该错误页面引用了JLS,Section 8.4.8.3,其中指出:

It is a compile-time error if a type declaration T has a member method m1 and there exists a method m2 declared in T or a supertype of T such that all of the following are true:

  • m1 and m2 have the same name.

  • m2 is accessible from T.

  • The signature of m1 is not a subsignature (§8.4.2) of the signature of m2.

  • The signature of m1 or some method m1 overrides (directly or indirectly) has the same erasure as the signature of m2 or some method m2 overrides (directly or indirectly).

这两种方法都没有另一种方法的子签名,因为它们都没有参数类型,Map< String,String>>和Map< String,String>是另一个的子类型.但是,他们有相同的删除,Map.

它永远不应该编译,但Java 7修复了Java错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读