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

查看:136
本文介绍了在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天全站免登陆