为什么一个班有效,而另一班没有?

编程入门 行业动态 更新时间:2024-10-28 15:27:01
本文介绍了为什么一个班有效,而另一班没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如你所见,具有非空返回类型很重要。

As you can see, having a non void return type is important.

class TestValid { public String f(List<String> list) { return null; } public Integer f(List<Integer> list) { return null; } public void test() { f(Arrays.asList("asdf")); f(Arrays.asList(123)); } } class TestInvalid { public void f(List<String> list) { System.out.println("strings"); } public void f(List<Integer> list) { System.out.println("numbers"); } }

推荐答案

TestValid 无效,开始:

TestValid.java:9: name clash: f(List<Integer>) and f(List<String>) have the same erasure public Integer f(List<Integer> list) { ^

所以返回类型是一个红色的鲱鱼如果在确定重载的签名时考虑到返回类型,那么它将有效,但目前它与以下无效:

So the return type is a red herring. If return types were taken into account in determining signatures for overloading, then it would be valid, but currently it's as invalid as this:

// Equally invalid public Integer f() {} public String f() {}

如果您真正设法获取 TestValid 进行编译,我将有兴趣知道您正在使用哪个编译器。

If you genuinely managed to get TestValid to compile, I'd be interested to know which compiler you're using.

更多推荐

为什么一个班有效,而另一班没有?

本文发布于:2023-05-25 10:59:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226681.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!