目录
一、方法的概述
1、定义
方法是一种语法结构,它可以把一段代码封装成一个功能,以便重复使用。
2、好处
提高代码的复用性;让程序逻辑更清晰;
3、相关问题
怎么定义方法;怎么调用方法;方法内存图;方法的参数传递机制;方法其他常见形式、技术(方法重载、递归);
二、方法的定义和调用
1、方法的完整定义形式、调用
//定义
修饰符 返回值类型 方法名(形参列表){
方法体代码(需要执行的功能代码)
return 返回值;
}
//调用
方法名(……);
2、方法的其他定义形式、调用
//定义
修饰符 返回值类型(void) 方法名(){
方法体代码(需要执行的功能代码)
(无return);
}
示例:
public static void print(){
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
}
3、注意事项
如果方法不需要返回结果,返回值类型必须声明为void(无返回值),此时方法内不可以使用return返回数据;
如果方法没有参数列表、或者返回值类型声明为void可以称为无参数,无返回值的方法,依次类推;
三、方法使用的常见问题
- 方法的编写顺序无所谓;
- 方法与方法之间是平级关系,不能嵌套定义;
- 方法的返回值类型为void(无返回值),方法内则不能用return返回数据类型;方法的返回值类型写了具体的类型,方法内部则必须使用return返回对应数据类型;
- return语句下面,不能编写代码,因为永远执行不到,属于无效代码;
- 方法不调用就不执行,调用时必须严格执行匹配对应参数情况;
- 有返回值的方法调用时可以选择定义变量接受结果,或者直接输出调用,甚至直接调用,无返回值的方法只能直接调用一下;
四、方法案例
1、定义方法的技巧
修饰符:public static
方法名称:自己取名,有意义,全英文,首字母小写,满足驼峰模式
方法体代码:完成自己需要写的功能代码即可(具体需求具体实现)
public static int add(int a,int b){
int c = a + b;
return c;
}
关注点:1、分析方法是否需要声明返回值类型;2、分析方法是否接受参数;
2、计算1~n的和,返回并判断是奇数还是偶数
import java.util.Scanner;
public class Demo{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("please input the value of the n:");
int n = sc.nextInt();
boolean flag = check(n);
if(flag){
System.out.println("the result is a odd number");
}else{
System.out.println("the result is a even number");
}
}
public static boolean check(int n){
int sum;
boolean flag;
for(int i = 1;i <= n;i++){
sum += i;
}
if(sum % 2 == 0){
flag = false;
}else{
flag = true;
}
return flag;
}
}
3、数组求最值案例改方法形式
public static int getArrayMaxData(int[ ] array){
int max = array[0];
for(int i = 1;i < array.length;i++){
if(max < array[i]{
max = array[i];
}
}
return max;
}
五、方法调用的内存图
- 方法没被调用时,在方法区中的字节码文件中存放;
- 方法被调用时,需要进入到栈内存中运行;
六、方法的参数传递机制
1、基本类型的参数传递
Java参数传递机制:值传递
值传递:在传输实参给形参的时候,并不是传输实参变量本身,而是传输实参变量中存储的值,这就是值传递。
注意:实参(如在方法内部定义的变量)
形参(如在定义方法时,“()”中所声明的参数)
2、引用类型的参数传递
传输的是存储的地址值。
七、方法的参数传递案例
1、打印数组内容 ![](https://img-blog.csdnimg.cn/1c3b65ca2aad41df8d3e55c17b4b55e0.png)
public class Demo{
public static void main(String args[]){
int[] array = {11,22,33,44,55};
print(array);
}
public static void print(int[] array){
System.out.print("[");
for(int i = 0;i < array.length - 1;i++){
System.out.print(array[i] + ",");
}
System.out.print(array[array.length] - 1 + "]");
}
}
2、从数组中查询元素的索引返回
import java.util.Scanner;
public class Demo{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("please input the length of the array:");
int n = scanner.nextInt();
int[] array = new int[n];
for(int i = 0;i < array.length;i++){
System.out.println("please input the " + (i + 1) + " number:");
array[i] = scanner.nextInt();
}
System.out.println("please input the number you want to search:");
int number = scanner.nextInt();
int result = function(array,number);
System.out.println("the result is:" + result);
}
public static int function(int[] array,int number){
int result = -1;
for(int i = 0;i < array.length;i++){
if(array[i] == number)
result = i;
}
return result;
}
}
3、比较两个数组内容是否一样
import java.util.Scanner;
public class Demo{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
System.out.println("please input the length of the array1:");
int n1 = s.nextInt();
int[] array1 = new int[n1];
function(array1);
System.out.println("please input the length of the array2:");
int n2 = s.nextInt();
int[] array2 = new int[n2];
function(array2);
boolean result = judge(array1,array2);
System.out.print(result);
}
public static void function(int[] array){
Scanner s = new Scanner(System.in);
for(int i = 0;i < array.length;i++){
System.out.println("please input the " + (i + 1) + " number:");
array[i] = s.nextInt();
}
}
public static boolean judge(int[] array1,int[] array2){
boolean result = true;
if(array1.length == array2.length){
for(int i = 0;i < array1.length;i++){
if(array1[i] != array2[i])
result = false;
}
}else{
result = false;
}
return result;
}
}
八、方法的重载
1、方法重载的形式
同一个类中,出现多个方法名称相同,但是形参列表是不同的,那么这些方法就是重载方法。
2、方法重载的识别技巧
只要是同一个类中,方法名称相同,形参列表不同,那么他就是重载方法,其他不管。
九、补充知识:单独使用return关键字
retrun:可以立即跳出当前并结束当前方法的执行;return关键字单独使用时可以放在任何方法中。