重载方法可能有不同的返回类型吗? [英] Is it possible to have different return types for a overloaded method?

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

问题描述

在方法重载中,重载方法是否可能具有不同的返回类型? 例如,

In method overloading, is it possible to have different return types for a overloaded method? for example,

void foo(int x) ;
int foo(int x,int y);
double foo(String str);

在一般的面向对象程序设计中,有可能吗?

in general object oriented programming, is it possible?

推荐答案

在一个类中,可以有几种方法共享相同的名称,但不同之处

In a class, there can be several methods sharing the same name but differ in

  1. 参数类型
  2. 参数数量
  3. 方法中声明的参数顺序

在运行时,编译器将根据为该方法提供的参数确定要执行该方法的版本.

By depending on the parameters provided for the method, in the run time, compiler determines which version of the method to execute.

重载的方法可能有也可能没有不同的返回类型.但是,仅返回类型不足以使编译器确定要在运行时执行哪种方法.

An overloaded method may or may not have different return types. But return type alone is not sufficient for the compiler to determine which method is to be executed at run time.

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

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