java 动手动脑 方法重载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
如下代码:<br>//MethodOverload.java
 //Using overloaded methods
 package HJssss;
public class zhuce {
 
    public static void main(String[] args) {
        System.out.println("The square of integer 7 is " + square(7));
        System.out.println("\nThe square of double 7.5 is " + square(7.5));
    }
 
    public static int square(int x) {
        return x * x;
    }
    public static double square(double y) {
        return y * y;
    }
}

  运行结果:

1
2
3
The square of integer 7 is 49
 
The square of double 7.5 is 56.25

上述代码展示了Java的方法重载特性。

满足以下条件的两个或多个方法构成“重载”关系:

1.方法名相同。

2.参数类型不同,参数个数不同,或者是参数类型的顺序不同。

注意:返回值不能作为方法重载的条件。

 

posted @   南北啊  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
1 2 3
4
点击右上角即可分享
微信分享提示