当前位置:网站首页>2021-04-16方法重载 传参
2021-04-16方法重载 传参
2022-06-23 09:55:00 【鹿其其鹿】
方法的重载
重载就是在一个类中,有相同的函数名称,但形参不同的函数
方法重载的规则:
- 方法名称必须相同
- 参数列表必须不同(个数不同,或类型不同,参数排序列表不同等)
- 方法的返回类型可以相同也可以不相同
- 仅仅返回类型不同不足以成为方法的重载
public class Demo3 {
public static void main(String[]args){
int max = max(10,20,30);
System.out.println(max);
}
//比大小
public static int max(double num1,double num2){
return 0;
}
//比大小
public static int max(int num1,int num2){
return 0;
}
//比大小
public static int max(int num1,int num2,int num3){
return 0;
}
}
命令行传参
有时候希望运行一个程序时候再给他消息,这要靠传递命令行参数给main()函数实现
可变参数
Java支持传递同类型的可变参数给一个方法
在方法声明中,在指定参数类型后加一个省略号
一个方法中只能指定一个可变参数,它必须是方法的最后一个参数,任何普通的参数必须在他之前声明
public class Demo4 {
public static void main(String[]args){
Demo4 demo4 = new Demo4();
demo4.test(1);
}
public void test (int...i){
System.out.println(i[0]);
}
}
边栏推荐
- Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file
- 陆奇首次出手投资量子计算
- 开发者,你对云计算可能有些误解
- Bugs encountered in Oracle
- After the uncommitted transactions in the redo log buffer of MySQL InnoDB are persisted to the redo log, what happens if the transaction rollback occurs? How does the redo log handle this transaction
- Cesium loading orthophoto scheme
- 有没有人,计划开源一套工业级“秒杀”系统架构?
- Multithreaded exercises
- ThinkPHP 2. X/3.0 vulnerability recurrence
- NIO例子
猜你喜欢

STM32F1与STM32CubeIDE编程实例-红外寻迹传感器驱动

文献综述怎么写 ,一直没头绪写不出来怎么办?

16. system startup process

利用华为云ECS服务器搭建安防视频监控平台

Jog sport mode

汇编语言中断及外部设备操作篇--06

Wechat applet: click the button to switch frequently, overlap the custom markers, but the value does not change

SQL create a new field based on the comparison date

Qiming Xingchen Huadian big data quantum security innovation laboratory was unveiled and two black technology products were released

卧槽,最惊艳的论文神器!
随机推荐
Developer, you may have some misunderstandings about cloud computing
【CTF】bjdctf_ 2020_ babyrop
AI: the Elephant in Room
XML related interview questions
JS教程之 什么是 JSX?为什么我们需要它?
135137138445 unable to remotely connect to the database due to disabling
[極客大挑戰 2019]HardSQL
[GXYCTF2019]BabyUpload
我被提拔了,怎么和原来平级的同事相处?
Is there anyone who plans to open source an industrial "seckill" system architecture?
beanstalk 常用方法、说明
NiO example
What is BFC? What problems can BFC solve
Gorm 高级查询
MySQL optimistic lock and pessimistic lock
2022 Gdevops全球敏捷运维峰会-广州站精华回放(附ppt下载)
Liujinhai, architect of zhongang Mining: lithium battery opens up a Xintiandi for fluorine chemical industry
Centre de calcul haute performance - nvme / nvme - of - nvme - of overview
swagger UI :%E2%80%8B
Use Base64 to show pictures