简体   繁体   English

Java编译器如何确定“方法不适用”?

[英]How does the java compiler determine 'method not applicable'?

When using a wildcards some methods (eg ArrayList.set ) cannot be used and return an error message similar to 使用通配符时,某些方法(例如ArrayList.set )无法使用,并返回类似于以下内容的错误消息

The method set(int, capture#3-of ?) in the type List is not applicable for the arguments (int, String) 类型List中的方法set(int,capture#3-of?)不适用于参数(int,String)

I have read why this is the case and would like to know how this is done. 我已经阅读了为什么会这样,并想知道如何完成。 That is how does the compiler know that some methods are applicable (eg List.get(int) ) but others are not? 那就是编译器如何知道某些方法适用(例如List.get(int) )而其他方法不适用?

所有适用的规则在Java语言规范#15.12中

Compiler assgins special tokens ( capture#3-of ? ) to each occurence of wildcard and use regular method applicability rules ( JLS 15.12.2 ). 每次出现通配符时,编译器都将特殊令牌( capture#3-of ?关联起来,并使用常规方法适用性规则( JLS 15.12.2 )。

Since captures of wildcards are not compatilbe with other types and with other captures, "method not applicable" is produced. 由于通配符的捕获不能与其他类型和其他捕获兼容,因此会产生“方法不适用”。

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

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