在 Java 中使用不同的返回类型重载? [英] Overload with different return type in Java?

查看:30
本文介绍了在 Java 中使用不同的返回类型重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不能仅通过更改返回类型来重载函数?这会在 Java 的未来版本中改变吗?

Why is it not possible to overload a function just by changing the return type? Will that change in a future version of Java?

顺便说一句,仅供参考,这在C++中可行吗?

By the way, just for reference, is this possible in C++?

推荐答案

Java 做不到,C++ 也做不到.理由是仅返回值不足以让编译器确定调用哪个函数:

You can't do it in Java, and you can't do it in C++. The rationale is that the return value alone is not sufficient for the compiler to figure out which function to call:

public int foo() {...}
public float foo() {..}

...
foo(); // which one?

这篇关于在 Java 中使用不同的返回类型重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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