Java学习笔记08方法_20200217

这篇博客详细介绍了Java中的方法,包括方法的定义、参数传递、返回值、方法重载以及变量作用域。通过实例解析了如何创建和使用方法,强调了方法重载的好处,同时探讨了类常量和类方法的使用。内容涵盖了从基本概念到实际应用的多个方面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在这里插入代码片

写自定义目录标题)

第8章 方法

01 习题

  1. 3000米长的绳子,每天减一半,问多少天这个绳子会小于5米。
		//3000米的绳子,每天减少一半,多少天小于5米
		int x = 3000;
		int count = 0;
		while(x >= 5) {
			x = x / 2;
			count++;
		}
		System.out.println(count);

在这里插入图片描述
另一种写法:

		int day = 1;
		int i = 3000;
		while(true) {
			i /= 2;
			
			System.out.println("第" + day + "天" + i);
			day++;
			if(i < 5) {
				break;
			}
		}

在这里插入图片描述

tip:
还可用右移,右移相当于除2。

  1. 打印99乘法表。
		//打印99乘法表
		//外循环控制行数,内循环控制算式的个数
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(i + "*" + j + "=" + (i * j) + " ");
			}
			System.out.println();
		}

在这里插入图片描述

02 方法的定义

方法是一段可以重复执行的代码块。
方法总是预先设计好,并且在被调用的时候执行。

  1. 方法的调用
public class day063 {
	//public 指示此方法是公有的
	//static 指示此方法是静态的
	//void 指示此方法没有返回值
	//printX是方法的名字,必须遵循标识符命名规则
	//printX方法不必在main方法之前定义
	//printX是提供打印直角三角形的功能	
	public static void printX() {
		//打印一个直角三角形
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
		
	public static void main(String[] args) {
		//调用printX方法
		//调用方法时,jvm会执行此方法的代码
		printX();
		//当print方法中的代码块执行结束后,才会执行main方法后续的代码
		System.out.println("第一个方法调用结束");
	}

}

在这里插入图片描述
2. 方法的重复调用

public class day063 {
	//public 指示此方法是公有的
	//static 指示此方法是静态的
	//void 指示此方法没有返回值
	//printX是方法的名字,必须遵循标识符命名规则
	//printX方法不必在main方法之前定义
	//printX是提供打印直角三角形的功能	
	public static void printX() {
		//打印一个直角三角形
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
		
	public static void main(String[] args) {
		//调用printX方法
		//调用方法时,jvm会执行此方法的代码
		printX();
		//当print方法中的代码块执行结束后,才会执行main方法后续的代码
		System.out.println("第一个方法调用结束");
		//再次调用 printX方法
		printX(); //方法可以被重复调用		
	}
}
_____________________________________________________
运行结果:
*
**
***
****
*****
第一个方法调用结束
*
**
***
****
*****

在这里插入图片描述
3. printX();和day063.printX(); 效果一样,
由于printX方法和当前的方法在同一个类,所以可以忽略掉类名。

public class day063 {
	//public 指示此方法是公有的
	//static 指示此方法是静态的
	//void 指示此方法没有返回值
	//printX是方法的名字,必须遵循标识符命名规则
	//printX方法不必在main方法之前定义
	//printX是提供打印直角三角形的功能	
	public static void printX() {
		//打印一个直角三角形
		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
		
	public static void main(String[] args) {
		//调用printX方法
		//调用方法时,jvm会执行此方法的代码
		printX();
		//当print方法中的代码块执行结束后,才会执行main方法后续的代码
		System.out.println("第一个方法调用结束");
		//再次调用 printX方法
		printX(); //方法可以被重复调用
		System.out.println("第二个方法调用结束");	
		//printX方法被声明为静态的,所以可以通过类名调用。
		day063.printX(); //由于printX方法和当前的方法在同一个类,所以可以忽略掉类名	
	}
}

______________________________________________________
运行结果
*
**
***
****
*****
第一个方法调用结束
*
**
***
****
*****
第二个方法调用结束
*
**
***
****
*****

在这里插入图片描述

03 方法参数

向方法传递参数

public class day064 {
	public static void printX() {
		//打印一个直角三角形
		for (int i = 1; i <= 6; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
			
		}
	}
		
	public static void main(String[] args){
		printX();		
	}
}

在这里插入图片描述

  1. 向方法传递参数
public class day064 {
	//int n声明了一个int类型的变量,这个变量叫做形式参数
	//当printX方法被调用时,n会接受实际参数的值。
	//形式参数在方法中作为变量为使用,它在被调用时赋值
	public static void printX(int n) {
		//打印一个直角三角形
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();			
		}
	}
	
			
	public static void main(String[] args){
		int x = 5;
		//x是实际参数,它的值将被拷贝给形式参数
		printX(x);	//打印5行的直角三角形
		
		x = 6;
		printX(x);// 打印6行的直角三角形
		
		printX(7); //打印7行的直角三角形
		
//		String c = "a";
//		printX(c); //编译错误,实际参数的类型必须要和形式参数匹配
		
		char c = 'a'; //97
		printX(c);  //正确的,char可以自动转换为int
				
	}
}

—————————————————————————————————————————————————————
运行结果
*
**
***
****
*****
*
**
***
****
*****
******
*
**
***
****
*****
******
*******
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
*************
**************
***************
****************
*****************
******************
*******************
********************
*********************
**********************
***********************
************************
*************************
**************************
***************************
****************************
*****************************
******************************
*******************************
********************************
*********************************
**********************************
***********************************
************************************
*************************************
**************************************
***************************************
****************************************
*****************************************
******************************************
*******************************************
********************************************
*********************************************
**********************************************
***********************************************
************************************************
*************************************************
**************************************************
***************************************************
****************************************************
*****************************************************
******************************************************
*******************************************************
********************************************************
*********************************************************
**********************************************************
***********************************************************
************************************************************
*************************************************************
**************************************************************
***************************************************************
****************************************************************
*****************************************************************
******************************************************************
*******************************************************************
********************************************************************
*********************************************************************
**********************************************************************
***********************************************************************
************************************************************************
*************************************************************************
**************************************************************************
***************************************************************************
****************************************************************************
*****************************************************************************
******************************************************************************
*******************************************************************************
********************************************************************************
*********************************************************************************
**********************************************************************************
***********************************************************************************
************************************************************************************
*************************************************************************************
**************************************************************************************
***************************************************************************************
****************************************************************************************
*****************************************************************************************
******************************************************************************************
*******************************************************************************************
********************************************************************************************
*********************************************************************************************
**********************************************************************************************
***********************************************************************************************
************************************************************************************************
*************************************************************************************************

在这里插入图片描述
3. 方法可以有多个参数

public class day065 {
	//方法可以有多个参数	
	//多个参数使用,分割。
	public static void printX(int n, char s) {	
		//打印一个直角三角形
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(s);
			}
			System.out.println();			
		}
	}
		
	public static void main(String[] args) {
		printX(8,'.'); //8传递给形式参数n,'.'传递给形式参数s
		
	}

}

在这里插入图片描述

在这里插入图片描述

04 方法的返回值

  1. 这个方法将计算start和end之间数字的和。
public class Day066_2 {
	// 这个方法将计算start和end之间数字的和
	// int是返回值的类型
	public static int sum(int start, int end) {
		int sum = 0;
		for (int i = start; i <= end; i++) {
			sum = sum + i;
		}
		return sum;// return语句将sum的值返回给调用者
	}

	public static void main(String[] args) {
		// 返回值会替代方法调用
		int result = sum(3, 5); // 调用方法,在调用结束后将返回的值赋值给result
		System.out.println(result);
	}
}

在这里插入图片描述

tip:

	public static void f1() {
		
	}
	
	//编译错误,如果方法声明了返回值,就必须返回一个值
	public static int f2() {
		
	}

如果方法声明了返回值,就必须返回一个值

方法实际返回的值要和声明的返回值类型匹配

强转是可以的,但是强转需要谨慎,因为可能导致数据丢失。
在这里插入图片描述
2. return 表示终止方法执行。

	//void方法也可以使用return
	public static void printX(int n) {
		//打印一个直角三角形
		if(n <= 0) {
			return; //return 表示终止方法执行
		}
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= i; j++) {
				System.out.println("*");
			}
			System.out.println();
		}
	}
	
	
	
	public static void main(String[] args) {		
		printX(-1);
		System.out.println("执行结束");
		
	}
}

在这里插入图片描述

  1. return语句后面不能再有其他代码。

以下代码是错误的,因为return语句后面不能再有其他代码,
但是需要注意的是:如里return语句在一个条件表达式中,后面就是可以有其他代码的,因为这个return语句有可能执行,也可能不执行。

	public static int f6() {
		return 0;
		System.out.println("Hello"); //编译错误,return语句后面不能再有其他代码
	}
	

在这里插入图片描述

在这里插入图片描述

05 方法重载

一个方法可以和另外一个方法有同样的方法名,但是必须有不同的参数列表,这种情况叫做方法重载。

  1. 例一
public class day067 {
	// 参数列表是空
	public static void f() {
		System.out.println("空的方法");
	}

	// 参数列表为int
	public static void f(int n) {
		System.out.println("一个int形参");
	}


	public static void main(String[] args) {
		f();
		f(1);

	}

}

在这里插入图片描述


错误示例:
参数列表为int,形参名不属于参数列表,所以会导致编译错误。
这时会提示:方法名重复
这样就不能重载了。
如下图:
在这里插入图片描述

在这里插入图片描述
下面代码可行
在这里插入图片描述

  1. 参数列表是方法所有参数类型的列表(不包括参数名),与参数类型,个数,顺序有关。

示例:返回最大值

public class Day068 {
	// 方法功能:返回最大值
	public static int max(int a, int b) {
		return a > b ? a : b;

	}

	// 方法功能:返回三个数中的最大数值
	public static int max(int a, int b, int c) {
		return a > b ? (a > c ? a : c) : (b > c ? b : c);

	}

	public static double max(double a, double b) {
		return a > b ? a : b;

	}

	public static void main(String[] args) {
		System.out.println(max(3, 5)); // 找max(int,int)
		System.out.println(max(3, 5, 6)); // max(int,int,int)
		System.out.println(max(4.5, 6.0)); // max(double,double)
	}

}

在这里插入图片描述
**tips: 重载的好处:只需要记一个方法名就可以了。

示例:调用字符类
在这里插入图片描述

示例:Math.max方法的重载。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

示例:println的重载
在这里插入图片描述
Ctrl+左键,看一下方法的源码
在这里插入图片描述
在这里插入图片描述

06 变量的作用域

变量的作用域决定了变量可以在什么地方(哪段代码)被访问。
在块中声明的变量(块变量)只能在当前块中使用。
在方法中声明的变量(本地变量)只能在当前方法中被使用。
在类中定义的静态变量(类变量)可以在当前类的任何地方使用。

  1. 例一
    在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

07 类常量和类方法

在类中使用satic声明的变量叫做类变量,使用static声明的方法叫做类方法。
在类中使用static final声明的叫做类常量。

public class Day0610 {
	//类常量
	static final double PI = 3.14;
	
	
	public static void main(String[] args) {
		System.out.println(Day0610.PI);
		System.out.println(PI);
		//public static final double PI = 3.14159265358979323846;
		System.out.println(Math.PI);  //使用Math类的类常量PI
		System.out.println(Math.max(6.5, 3.5)); //调用Math类的类方法max
	}

}
____________________________________
3.14
3.14
3.141592653589793
6.5

在这里插入图片描述

导入
在这里插入图片描述
导入成功
在这里插入图片描述
由于没有public,报错Day0610.PI不可见
在这里插入图片描述
解决
转到Day0610.java中,
把static final double PI = 3.14;
添加成 public static final double PI = 3.14;
在这里插入图片描述

调Day069.java中的f方法:
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值