Java方法重载

package com.gzxh.chapt02;

public class MethodDemo {

	/**
	 * @param args
	 * 方法的重载
	 * 1.方法名字相同
	 * 2.参数个数不同
	 * 3.参数数据类型不同
	 * 方法重载跟返回值类型无关
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		
		System.out.println(add2(3,6));
		System.out.println(add2(3,8,6));
		System.out.println(add2(2.2,4.6));
		show();
		show(2.5);
		show("呵呵");
	}
	//做加法运算:两个int型参数
	static int add2(int a,int b){
		int res;
		res = a + b;
		return res;
	}
	static int add2(int a,int b,int c){
		return a+b+c;
	
	}
	static double add2(double a,double c){
		return a+c;
	}
	static void show(){
		System.out.println("我太难了");
	}
	static void show(double age){
		System.out.println(age+"我太难了");
	}
	static void show(String name){
		System.out.println(name+"我太难了");
	
	}
}

package com.gzxh.chapt02;

public class Method {

	/**
	 * @param args
	 * 方法(函数):可以重复调用的代码。
	 * 修饰符   返回值类型(有返回值)/void(没有返回值) 方法名(参数据类型 参数1,参数数据类型 参数2.....){
	 * 		//执行代码
	 * 		return 返回值;(有返回值)
	 * }
	 */
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		int x = add();
		
//		show();//直接调用
//		show();
//		show();
//		show();
//		show2("小明");
//		show3(3);
		add();
		System.out.println(x);
		int y = add2(3,6);
		System.out.println(y);
	}
	static void show(){
		System.out.println("我太难了");
	}
	
	static void show2(String name){
		System.out.println(name+"我太难了");
	}
	static void show3(int age){
		System.out.println(age+"我太难了");
	}
	static int add(){
		System.out.println("优秀");
		return 0;
	}
	static int add2(int a,int b){
		System.out.println("优秀");
		int res;
		res = a + b;
		return res;
	}
	
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值