亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

泛型類型推斷不能與方法鏈接一起使用?

泛型類型推斷不能與方法鏈接一起使用?

DIEA 2019-10-12 10:12:23
該代碼在Java 8中編譯,但在Java 7中無法編譯:class Map<K,V> {    static <K,V> Map<K,V> empty() {return null;}    Map<K,V> put(K k, V v) {return null;}    V get(K k) {return null;}}class A {    static void f(Map<Integer,String> m){}    public static void main(String[] args) {        f(Map.empty());    }}它不會推斷Map從中返回的具體類型Map.empty():$ javac7 A.javaA.java:10: error: method f in class A cannot be applied to given types;        f(Map.empty());        ^  required: Map<Integer,String>  found: Map<Object,Object>  reason: actual argument Map<Object,Object> cannot be converted to Map<Integer,String> by method invocation conversion1 error如果將f調用更改為,它將編譯f(Map.<Integer,String>empty());。在Java 8中,它可以工作而不必求助于此。但是,如果將f調用更改為f(Map.empty().put(1,"A").put(2,"B"));,它將無法在Java 7和8上再次編譯。為什么?$ $javac7 A.java A.java:10: error: method f in class A cannot be applied to given types;        f(Map.empty().put(1,"A").put(2,"B"));        ^  required: Map<Integer,String>  found: Map<Object,Object>  reason: actual argument Map<Object,Object> cannot be converted to Map<Integer,String> by method invocation conversion1 error$ $javac8 A.javaA.java:10: error: incompatible types: Map<Object,Object> cannot be converted to Map<Integer,String>        f(Map.empty().put(1,"A").put(2,"B"));                                    ^Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output1 error$ $javac8 -Xdiags:verbose A.javaA.java:10: error: method f in class A cannot be applied to given types;        f(Map.empty().put(1,"A").put(2,"B"));        ^  required: Map<Integer,String>  found: Map<Object,Object>  reason: argument mismatch; Map<Object,Object> cannot be converted to Map<Integer,String>1 error
查看完整描述

1 回答

  • 1 回答
  • 0 關注
  • 342 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號