Java:方法重载

 时间:2026-04-22 18:07:23

1、方法重载是两种多态的一种,我们可以向方法传递不同的消息,以便让对象根据相应的消息来产生相应的行为。

这里定义了三个方法。根据方法的类型,以及方法的参数列表和参数的数目来判断方法是否重载。

Java:方法重载

Java:方法重载

Java:方法重载

2、但这里需要注意的是,参数的名字不参与比较,所以在方法中,int a,和int b与int  m以及int n是没有区别的,如果这样定义程序会产生错误。

Java:方法重载

Java:方法重载

Java:方法重载

3、附代码:

class People{ float hello(int a ,int b){ return a+b; } float hello(long n,int m){ return a-b; } double hello(double a,int b){ return a*b; }}public class E17{ public static void main(String args[ ]){ People tom = new People(); System.out.println(tom.hello(10,20)); System.out.println(tom.hello(10L,20)); System.out.println(tom.hello(10.0,20)); }}

Java:方法重载

Java:方法重载

Java:方法重载

  • 我的侠客长生门不传之秘怎么学
  • 觅长生怎么用神秘绿瓶
  • 部落与弯刀怎么招募蝎后
  • 觅长生怎么保存游戏
  • 觅长生新手前期攻略
  • 热门搜索
    什么叫有机奶 应和的意思 dwg格式用什么打开 发奋图强的意思 黑皮肤穿什么颜色 pcm是什么意思 什么都不做 我的世界萤石有什么用 贪婪的意思 什么枪战游戏好玩