Java基础系列之方法(函数)

方法 (函数)
方法的作用:提高代码的复用性。(循环也可以提高代码的复用性,但是他只针对局部代码去重复,方法可以对全局去重复)

		方法的使用1-基本使用:
			基本格式 :
				public static void 方法名(){
					方法体;//很多条语句
				}

				public static 修饰符 固定死的
				void :代表这个方法没有返回值的。
				方法名 : 标识符   
							a-zA-Z0-9$_
							不能是关键字
							不能以数字开头
							
							大驼峰 类名接口名
							小驼峰 变量名 方法名
							峡谷先锋 常量名
			
			注意事项:
				1:方法定义之后,不调用不执行的(main方法才是程序的入口,所以只能在main中直接或者间接被调用才能执行)
					public class Demo3 {
						public static void main(String[] args) {
							show();
						}
						public static void show(){
							System.out.println("hellowrold");
							method();
						}

						public static void method(){
							System.out.println("method");
						}
					}
				2:方法和方法之间是独立的 不能嵌套
				3:方法的定义 是没有前后顺序之分的
				4:main方法调用另一个方法,等待着另一个方法执行结束之后 main才继续的
					public class Demo3 {
						public static void show(){
							System.out.println(2);
							System.out.println(3);
							System.out.println(4);

						}

						public static void main(String[] args) {
							System.out.println(1);
							show();
							System.out.println(5);
							//打印结果:12345
						}
					}
				
			案例:定义一个方法,判断9这个数 是奇数 还是否书  如果是奇数 就打印奇数,如果是偶数就打印偶数
				public class Demo6 {
					public static void main(String[] args) {

						judge9OddEven();
					}

					public static void judge9OddEven(){
						/*
						if (9%2==0){
							System.out.println("偶数");
						}else {
							System.out.println("奇数");
						}
						*/

						// if else 如果否则 这个逻辑结构 和三元运算符 非常类型
						// 条件表达式 ? 表达式1 : 表达式2

						//9%2==0 ? System.out.println("偶数")  : System.out.println("奇数") ;
						// 表达式 不能单独成语句。
						//int a = 9%2==0 ? System.out.println("偶数")  : System.out.println("奇数") ;
						//String a = 9%2==0 ? System.out.println("偶数")  : System.out.println("奇数") ;
						//System.out.println(9%2==0 ? System.out.println("偶数")  : System.out.println("奇数"));

						//表达式1  表达式2 无论多么复杂 但是你必须有一个结果。

						/*
						三元的两个限制:
							1: 三元表达式 不能单独成语句
							2: 表达式1  表达式2 无论多么复杂 但是你必须有一个结果。
						 */

						/*
						if else 就没有这个限制 随便用
						 */

						// 用三元求两个数的最大值
						int a = 10;
						int b = 20;
						int max = a>b? a:b;


						int max1;
						if(a>b){
							max1 = a;
						}else {
							max1 = b;
						}

						// 总结 能用三元的地方 一定可以用ifelse, 但是 能用ifelse的地方不见得能用三元。

						//根据三元的两个限制条件, 我就想使用三元 打印奇数或者偶数。

						System.out.println(9%2==0 ? "偶数"  : "奇数" );
					}
				}
		
		方法的使用2-参数使用:
			参数位置 :
				public static void 方法名(数据类型 变量名,数据类型 变量名,数据类型 变量名,.... ){
					方法体;//很多条语句
				}
			
			案例演示:
				public class Demo5 {
					public static void main(String[] args) {
						//judge9JiOu();

						judge9JiOu(10);

						judge9JiOu(11);

						int c = 8;
						judge9JiOu(c);

						byte b = 3;
						judge9JiOu(b);

						long lo = 100L;
						//judge9JiOu(lo); //编译报错 8个字节的大小 不能放进去的。
						judge9JiOu((int)lo);

						int a = 5;
						judge9JiOu(a);

					}

					public static void judge9JiOu(int a){   // a就相当于是个托盘  4个单位的大小。
						System.out.println(a%2==0?"偶数":"奇数");
					}
				}
				
				public class Demo6 {
					public static void main(String[] args) {
						judgeJiOu(10,11);

						int c = 100;
						int d = 110;
						judgeJiOu(c,d);

					}
					public static void judgeJiOu(int a , int b){
						System.out.println(a+"是"+ (a%2==0?"偶数":"奇数"));
						System.out.println(b+"是"+ (b%2==0?"偶数":"奇数"));
					}
				}

			案例1:判断一个年龄 是否成年,如果成年了打印可以进入网吧,如果不成年 就说未成年人禁止入内。
				年龄不能写死,  成年年龄也不能写死
				public class Demo7 {
					public static void main(String[] args) {
						judgeAdultAge(19,18);
						judgeAdultAge(10,18);

						judgeAdultAge(10,8);
						judgeAdultAge(10,9);
					}

					//案例1:判断一个年龄 是否成年,如果成年了打印可以进入网吧,如果不成年 就说未成年人禁止入内。
					//				年龄不能写死,  成年年龄也不能写死
					public static void judgeAdultAge(int age , int adultAge){
						if (age >= adultAge){
							System.out.println("可以进入网吧");
						}else {
							System.out.println("未成年人禁止入内");
						}
					}
				}
			
			案例2:定义一个方法 打印 一个数到另一个数之间所有的奇数。
				这两个数是你传入进来的。
				public class Demo8 {
					public static void main(String[] args) {
						showJi(10,20);
						showJi(20,10);
					}

					//案例2:定义一个方法 打印 一个数到另一个数之间所有的奇数。
					//				这两个数是你传入进来的。
					public static void showJi(int m , int n){
						if (n>m){
							for (int i = m; i <= n; i++) {
								if (i%2!=0){
									System.out.println(i);
								}
							}
						}else{
							for (int i = n; i <= m; i++) {
								if (i%2!=0){
									System.out.println(i);
								}
							}
						}
					}
				}
		
			案例3:定义一个方法 判断两个数是否相等。 打印true 或者false
			
		方法的使用3-返回值使用:
			格式 :
				public static 数据类型 方法名(数据类型 变量名,数据类型 变量名,数据类型 变量名,.... ){
					方法体;//很多条语句
					return 返回值;
				}
			
			举例:
				public class Demo10 {
					public static void main(String[] args) {
						equals(10,11);  // 没有意义
						//false;

						boolean b = equals(20,21);
						System.out.println(b);

						System.out.println( equals(20,20) );

					}

					public static boolean equals(int a , int b){
						/*if (a==b){
							return true;
						}else {
							return false;
						}*/
						/*boolean c = a==b? true:false;
						return c;*/

						//return  a==b? true:false;

						return a==b;
					}
				}

				public class Demo11 {
					public static void main(String[] args) {
					   // byte method = method();
						 int c = method();
						System.out.println(c);
					}

					public static int method(){  // int 这是出去的口  这个口的大小 是可以让4个字节的数据 出去的。
						/*int a = 10;
						return a;*/

						/*byte b = 1;
						return b;*/

						double d = 3.14;
						return (int)d;

					}
				}

				public class Demo9 {
					public static void main(String[] args) {
						// 一个年龄 个 结婚年龄 比较。
						int ag = 17;
						int ag1 = 22;

						//boolean b = judgeAdultAge(17,22);
						//if (b){
						if(judgeAdultAge(17,22)){
							System.out.println("可以结婚");
						}else{
							System.out.println("玩去");
						}
					}

					public static boolean judgeAdultAge(int age , int Age1){  // 仅让这个方法 干专门的 判断的事情,
						if (age >= Age1){
							return true;
						}else {
							return false;
						}
					}
				}
			
			案例2:定义一个方法 传入三个数  返回三个数的最大的值。
				public class Demo12 {
					public static void main(String[] args) {
						int max = getMax(10, 30, 20);
						System.out.println(max);
					}

					//案例2:定义一个方法 传入三个数  返回三个数的最大的值。
					public static int getMax(int a, int b ,int c){
						/*int max = a>b ? (a>c?a:c) :(b>c?b:c);
						return max;*/
						return a>b ? (a>c?a:c) :(b>c?b:c);
					}
				}
			
		方法的完整格式:
			public static 数据类型/void 方法名(数据类型 变量名, 数据类型 变量名  ,.....){
				很多语句;
				return 返回值;
			}

			定义的时候 明确两个:
				参数:定义这个机器(方法),需要有原材料(参数) 就写参数。
				返回值类型 : 机器(方法) 直接结束之后,需要有一些产品 返回出来,那么就写返回值,否则就不用写返回值类型。

		方法的注意事项:
			1:方法不能嵌套
			2:方法和方法之间是没有先后顺序的。
			3:方法不调用不会执行的,  因为我们程序的入口都是main方法。
			4:void的方法,说明方法内是不能有返回值的,  但是你是可以写一个 单独的return的。用来强制终止方法。
				return 这个关键字的作用, 两个作用 
					第一个:return 强制终止方法的作用。
					第二个: return 返回值, 把 值扔给调用者 

			5:如果一个方法有返回值类型, 那么这个方法 则必须各个逻辑上,都应该有返回值。
				案例1:
					public static int getInt(int a ){
					  if(a> 10){
						return a ;
					  }
					} // 编译报错  因为 if 有可能不进去 当不进去的时候 这么方法就不会执行return , 然后你的方法是有返回值类型的 必须要有return 。
				
				案例2:
					public static int getInt(int a ){
						if(a> 0){
							return a ;
						}else{
							return 0-a;
						}
					} // 编译正确。 要么进入 if  要么进入else  所以这个方法是肯定会执行return的。

				案例3:
					public static int getInt(int a ){
						if(a> 0){
							return a ;
						}
						return 0-a; 
					}// 编译正确:因为return有强制终止方法的作用, 所以 一个方法要是规定了返回值类型了。那么这个方法 是肯定只有一个返回值的。
				
				案例4:
					public static int getInt(int a ){
						if(a> 0){
							return a ;
						}else if(a==0){
							return 0;
						}else if(a<0){
							return 0-a;
						}
					}  // 编译错误:虚拟机不会去看你值的逻辑, 他只看你结构语句的逻辑。
				
				案例5:
					public static int getInt(int a ){
						if(a> 0){
							return a ;
						}else if(a==0){
							return 0;
						}else{
							return 0-a;
						}
					} // 编译正确:这个结构语句的逻辑是完善的。
			
		
		方法的重载:
			概念:同一个类中,出现了方法名字相同,参数列表不同(返回值无关)这些方法之间重载了。

			举例:
				class Demo {
					public static void show(int a){
					
					}
					/*
					public static void show(int b){  // 方法重复了。  
					
					}
					*/
					/*
					public static int show(int a){  //方法重复了。
						return 0;
					}
					*/
					public static void show(double a){  // 重载   类型不同
					
					}

					public static void method(int a, double b){
					
					}
					/*
					public static void method(int b, double a){// 方法重复了
					
					}*/
					public static void method(double a, int b){ //重载  类型的顺序不同
					
					}

					public static void method( int a){ //重载  个数不同
					
					}
				}
			
			好处:给程序员减轻了记忆的压力。
				System.out.println(1);
				System.out.println(10.2);
				System.out.println(true);

			案例1:定义几个重载的方法。 方法内容是比较两个数是否相等。  要求 试用于 所有的基本类型。
				public class Demo14 {
					public static void main(String[] args) {
						char c = 'a';
						char c1 = 'a';
						System.out.println(compare(c,c1));

						boolean b = true;
						boolean d = false;
						System.out.println(compare(b,d));


					}
					//定义几个重载的方法。 方法内容是比较两个数是否相等。  要求 试用于 所有的基本类型。
					/*
						byte < short = char <int <long <float <double
						boolean
					 */
					public static boolean compare(double d, double d1){ // double d= 'a'; //97.0
						return d ==d1;
					}

					public static boolean compare(boolean d, boolean d1){
						return d ==d1;
					}
				}
				
		方法参数的传递:
			案例1:
				public class Demo14 {
					public static void main(String[] args) {
						int a = 10;
						System.out.println(a); // 10
						change(a);
						System.out.println(a); // 10

					}

					public static void change(int a){   
						a = 20;
					}
				}

			案例6:
				public class Demo14 {
					public static void main(String[] args) {
						int a = 10;
						System.out.println(a); // 10
						a = change(a);
						System.out.println(a); // 20

					}

					public static int change(int a){   
						a = 20;
						return a;
					}
				}
			
			案例2:
				public class Demo14 {
					public static void main(String[] args) {
						int[] arr = {10,20,30};
						System.out.println(arr[1]); // 20
						change(arr);
						System.out.println(arr[1]); // 200
					}

					public static void change(int[] arr){   
						arr[1] = 200;
					}
				}
			
			案例5:	
				public class Demo14 {
					public static void main(String[] args) {
						int[] arr = {10,20,30};  //0x001
						System.out.println(arr[1]); // 20
						arr = change(arr); // arr = 0x001
						System.out.println(arr[1]); // 200
					}

					public static int[] change(int[] arr){    //arr = 0x001
						arr[1] = 200;
						return arr;  //0x001
					}
				}
			
			案例3:
				public class Demo14 {
					public static void main(String[] args) {
						int[] arr = {10,20,30};
						System.out.println(arr[1]); // 20
						change(arr);
						System.out.println(arr[1]); // 20
					}

					public static void change(int[] arr){   
						arr = new int[]{100,200,300};
					}
				} // 只有引用类型 传递到另一个方法里面, 去操作了这个引用类型里面的内容的时候, 才会改变的。
			
			案例4:
				public class Demo14 {
					public static void main(String[] args) {
						int[] arr = {10,20,30};  //oo1 
						System.out.println(arr[1]); // 20
						arr = change(arr); // arr=002
						System.out.println(arr[1]); // 200
					}

					public static int[] change(int[] arr){   //001 
						arr = new int[]{100,200,300};  //002
						return arr; //002
					}
				} 

			结论:
				是不要片面理解 ,要学会自己分析。

		案例1:定义一个方法 传递一个 int[] 数组,  按照指定的格式打印出数组
						// int[] arr = {4,5,6,8};
			public static void show(int[] arr){
			
				// 显示 [4, 5, 6, 8]
			}

		   --------------------------------------------------------------
			public class Demo19 {
				public static void main(String[] args) {
					int[] arr = {6,8,3,5};
					printArr(arr);
				}

				//练习1: 定义一个方法 传入一个int[] 遍历数组。
				/*
				参数: int[] arr
				返回值类型: void
				 */
				public static void printArr(int[] arr){
					System.out.print("[");
					for (int i = 0; i < arr.length; i++) {
						if (i ==arr.length-1){
							System.out.println(arr[i]+"]");
							break;
						}
						System.out.print(arr[i]+", ");
					}
				}
			}
		练习2: 定义一个方法 传入一个int[] 返回数组的最大值。
			public class Demo20 {
				public static void main(String[] args) {
					int[] arr = {5,7,9,3,2};
					int max = getMax(arr);
					System.out.println(max);
				}

				// 定义一个方法 传入一个int[] 返回数组的最大值。
				/*
				参数: int[] arr
				返回值类型: int
				 */
				public static int getMax(int[] arr){
					int max = arr[0];
					for (int i = 1; i < arr.length; i++) {
						if (arr[i]> max){
							max = arr[i];
						}
					}
					return max;
				}
			}
		练习3: 定义一个方法 传入一个int[] 返回数组的 最大和最小值。	
			public class Demo20 {
				public static void main(String[] args) {
					int[] arr = {5,7,9,3,2};
					int[] brr = getMaxAndMin(arr);
					System.out.println(brr[0]);
					System.out.println(brr[1]);
				}

				// 定义一个方法 传入一个int[] 返回数组的最大值。
				/*
				参数: int[] arr
				返回值类型: int
				 */
				public static int[] getMaxAndMin(int[] arr){
					int max = arr[0];
					int min = arr[0];
					for (int i = 1; i < arr.length; i++) {
						if (arr[i]> max){
							max = arr[i];
						}

						if (arr[i] < min){
							min = arr[i];
						}
					}
					int[] arr1 = {max, min};
					return arr1;
				}
			}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拥你入怀、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值