Java:方法重载

 时间:2026-02-16 18:46:39

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:方法重载

  • 我的侠客长生门不传之秘怎么学
  • 觅长生怎么用神秘绿瓶
  • 部落与弯刀怎么招募蝎后
  • 觅长生怎么保存游戏
  • 觅长生新手前期攻略
  • 热门搜索
    疤痕疙瘩怎么治疗 青春痘怎么快速消除 怎么做网站推广 游戏怎么赚钱 痛经怎么治疗 销售计划怎么写 康师傅怎么了 难度系数怎么算 叉子用英语怎么说 excel怎么画斜线