Java——方法
一、定义
可以把一段代码封装成一个功能,以便重复调用。
二、方法格式
(一)有返回值和形参的方法
1.格式
修饰符(public static) 返回值类型 方法名(形参列表){
方法体代码(需要执行的功能代码)
return 返回值;
}
2.举例
import java.util.Scanner;
public class HelloWorld {public static void main(String[] args) {Scanner input = new Scanner(System.in);int a= input.nextInt();int b= input.nextInt();
#调用方法int ans=sum(a,b);System.out.println(ans);}
#写入方法public static int sum (int a,int b){int c=a+b;return c;}}
//结果
//10
//50
//60
3.注意
①方法的修饰符:暂时都使用public static修饰 。
②方法申明了具体的返回值类型,内部必须使用return放回对应类型的数据。
③形参列表可以有多个,甚至可以没有;如果有多个形参,多个形参必须用 “,” 隔开,且不能给初始值。
(二)无返回值和参数的方法
1.格式
修饰符(public static) void 方法名(){
方法体代码(需要执行的功能代码)
}
2.举例
public class HelloWorld {public static void main(String[] args) {
#调用方法helloworld();}
#编写方法public static void helloworld (){for (int i = 1; i <=3 ; i++) {System.out.println("Hello World!");}}
}
//结果
//Hello World!
//Hello World!
//Hello World!
3.注意
①如果方法不需要返回数据,返回值类型必须申明成void,此时方法内部不可以使用return返回数据。
②方法如果不需要接收数据,则不需要定义参数,且调用方法时也不可以传数据给方法。
(三)有参数无返回值的方法
1.格式
修饰符(public static) void 方法名(参数列表){
方法体代码(需要执行的功能代码)
}
2.举例
import java.util.Scanner;
public class HelloWorld {public static void main(String[] args) {Scanner input = new Scanner(System.in);
#输入要打印“Hello World!”的次数int a= input.nextInt();
#将参数a传入方法helloworld(a);}
#编写方法public static void helloworld (int n){for (int i = 1; i <=n ; i++) {System.out.println("Hello World!");}}
}
//结果
//4
//Hello World!
//Hello World!
//Hello World!
//Hello World!
3.注意
①如果方法不需要返回数据,返回值类型必须申明成void,此时方法内部不可以使用return返回数据。
三、方法使用时的常见问题
1.方法在类中的位置放前放后无所谓,但一个方法不能定义在另一个方法里面。
import java.util.Scanner;
public class HelloWorld {
#helloworld方法public static void helloworld (int n){for (int i = 1; i <=n ; i++) {System.out.println("Hello World!");}}
#main方法public static void main(String[] args) {Scanner input = new Scanner(System.in);int a= input.nextInt();helloworld(a);}
}
//结果
//1
//Hello World!
2.return语句的下面不能编写代码,属于无效的代码,执行不到这。
3.方法不调用就不会执行,调用方法时,传给方法的数据,必须严格匹配方法的参数情况。
4.调用有返回值的方法,有3种方式:①可以定义变量接收结果②或者直接输出调用③甚至直接调用。
public class HelloWorld {public static void main(String[] args) {
// 定义变量接收结果int a=sum(10,20);System.out.println(a);
// 直接输出调用System.out.println(sum(10, 20));
// 直接调用(不会输出结果)sum(10,20);}
//编写代码public static int sum(int x,int y){int ans=x+y;return ans;}
}
//结果
//30
//30
5.调用无返回值的方法,只有一种方式:①只能直接调用。
import java.util.Scanner;
public class HelloWorld {
#helloworld方法public static void helloworld (int n){for (int i = 1; i <=n ; i++) {System.out.println("Hello World!");}}
#main方法public static void main(String[] args) {Scanner input = new Scanner(System.in);int a= input.nextInt();
//直接调用(因为没有返回值)helloworld(a);}
}
//结果
//1
//Hello World!
四、定义一个方法是要关注的点
1.方法是否需要接收数据?是否需要定义形参列表。
2.方法是否需要返回数据?是否需要声明具体的返回值类型。
五、参数传递机制(值传递)
1.值传递:即在传输实参给方法的形参时,传输的是实参变量中存储的值的副本。
2.实参:在方法内部定义的变量。
3.形参:定义方法时所声明的参数。
(一)基本类型的参数传递
1.基本类型的参数传输存储的数据值。
2.举例
public class HelloWorld {public static void main(String[] args) {
// 实参——aint a=10;change(a);System.out.println("main:"+a);}
// 形参——a(因为a已经在编写方法时已经定义了类型,所以在a=20这一行时不用重新定义类型)public static void change(int a){System.out.println("change1:"+a);
// 将形参a重新赋值a=20;System.out.println("change2:"+a);}
}
//结果
//change1:10
//change2:20
//main:10
(二)引用类型的参数传递
1.引用类型的参数传输存储的地址值。
2.举例
public class HelloWorld {public static void main(String[] args) {Scanner input = new Scanner(System.in);
// 定义引用类型的实参int[] arrs = new int[]{10,20,30};
// 将引用类型实参的地址副本传给方法。change(arrs);System.out.println("main:"+arrs[1]);}
// 定义一个引用类型形参public static void change(int[] arrs){System.out.println("change1:"+arrs[1]);
// 因为实参和形参的地址是一样的,所以会改变实参的值arrs[1]=222;System.out.println("change2:"+arrs[1]);}
}
//结果
//change1:20
//change2:222
//main:222
六、方法重载
(一)定义
一个类中,出现多个方法的名称相同,但是它们的形参列表是不同的, 那么这些方法就称为方法重载了。
举例:
public class HelloWorld {public static void main(String[] args) {test();test(20);}public static void test(){System.out.println("=========tsst1===========");}public static void test(int a){System.out.println("==========test2========="+a);}
}
//结果
//=========tsst1===========
//==========test2=========20
(二)注意事项
1.一个类只要一些方法的名称相同、形参列表不同,那么它们就是方法重载了,其它的都不管(如:修饰符,返回值类型是否一样都无所谓)。
2.形参列表不同指的是:形参的个数、类型、顺序不同,不关心形参的名称。
七、return关键字
(一)使用情况:
用于从方法中返回一个值或终止方法的执行
(二) 在返回一个值的情况下
1.前提:在方法中声明了一个返回类型(非void
)(则必须使用return
关键字返回一个与返回类型匹配的值)
2.举例
public class HelloWorld {public static void main(String[] args) {int ans=test(10,20);System.out.println(ans);}
// 在方法test中,声明了要返回整数类型的值,所以必须要有return关键字来返回值。public static int test(int a,int b){int c=a+b;return c;}
}
//结果
//30
(三)在终止方法执行的情况下
1.前提:
在方法中不需要返回值(为void
),可以使用return立即跳出并结束当前方法的执行。
2.举例
public class HelloWorld {public static void main(String[] args) {chu(10,0);}public static void chu(int a,int b){if (b == 0) {System.out.println("除数不能为0.");return;}int c=a/b;System.out.println(a+"/"+b+"="+c);}
}
//结果
//除数不能为0.