为什么一个班有效,而另一班没有? [英] Why is one class valid while the other is not?

查看:140
本文介绍了为什么一个班有效,而另一班没有?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于为什么一个班有效,而另一班没有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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