Java之方法重载篇(我重载了,你要如何来调用我。。)
2024-10-19 05:30:22
# **一、课前引言**
请看一下代码,你发现什么特殊之处了吗?
public class MethodOverload {
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;
}
}
二、如何重载?如何调用?
上述示例代码展示了Java的“方法重载(overload)”特性。
重载的条件
- 两个或多个方法才能构成“重载”关系;
- 方法名一致;
- 参数类型不同,参数个数不同,或者是参数类型的顺序不同;
需要注意
方法的返回值不作为方法重载的判断条件。
查看JDK文档中System.out.println()方法,发现了什么?
共有10个println()方法构成了“重载关系”,如下图:
4. ## 如何调用?
由课前引言中的示例可知,调用具有“重载关系”的方法时,传递的实参类型决定了所调用的方法。
当实参类型是整型,那么系统会相应的调用形参同样是整型的int square(int x)方法;当实参类型是浮点型,那么系统会相应的调用形参同样是整型的int square(double y)方法;
方法调用跟形参变量名(x,y)没有关系,与形参类型有关。最新文章
- 基本数据结构(1)——算法导论(11)
- java编程题
- vs2010连接postgresql数据库
- 161021、spring异步调用,完美解决!
- POJ2531——Network Saboteur(随机化算法水一发)
- hdoj 3849 By Recognizing These Guys, We Find Social Networks Useful【双连通分量求桥&;&;输出桥&;&;字符串处理】
- mysqli 扩展库的预处理技术(mysqli_stmt)
- uboot环境变量区为何不能放在data段
- HTML5 Canvas Text实例1
- OBJ-C
- 你好,C++(19)“老师,我这次四级考试过了没有?”——4.2 条件选择语句
- Spring学习之常用注解(转)
- .NET Mvc Razor
- python 带小数点时间格式化
- Spring Boot 整合Quartz定时器
- learning mqtt protocol
- 使用蒲公英路由器 X3 设置为网络中继器
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
- MariaDB 和 MySQL 比较
- csu 1982:小M的移动硬盘(双向链表)
热门文章
- 【效率】专为Win7系统设计的极简番茄计时器 - MiniPomodoro (附源码)
- WCF 的 Service Instance模式和并发处理
- [大数据之Sqoop] —— 什么是Sqoop?
- sun.misc.BASE64Encoder找不到jar包的解决方法
- Python第一天 - list\字符串截取
- MVC5 网站开发实践 概述
- javascript运动系列第七篇——鼠标跟随运动
- 深入理解DOM节点类型第四篇——文档片段节点DocumentFragment
- JavaScript变量声明提前
- 窥探Swift之函数与闭包的应用实例