方法
1.什么是方法
方法(method)是程序中最小的执行单元(方法中的语句要么全都一起执行,要么全都一起不执行)。
方法有什么用
①提高代码的复用性
②提高代码可维护性
2.方法的格式
把一些代码打包在一起(方法定义),用到的时候就调用(方法调用)。
方法定义和调用
1)最简单的方法定义和调用
①定义
格式
public static void 方法名(){//main方法的外面,类的里面
方法体;//就是打包起来的代码
}
②调用
格式
方法名();//先定义再调用
看代码说结果1
public class Test1 {
public static void main(String[] args) {
System.out.println("a");
method();
System.out.println("b");
}
public static void method() {
System.out.println("c");
System.out.println("d");
}
}
总结:
看到方法进入方法
执行完毕回到调用处
看代码说结果2
public class Test2 {
public static void main(String[] args) {
System.out.println("a");
method1();
System.out.println("b");
}
public static void method1() {
method2();
System.out.println("c");
}
public static void method2() {
System.out.println("d");
System.out.println("e");
}
}
人肉计算器1
需求:定义一个方法,在方法内部定义两个变量。
求出它们的和并打印。
import java.util.Scanner;
public class Calculator1 {
public static void main(String[] args) {
method();
}
public static void method() {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first number to be plused.");
int number1 = sc.nextInt();
System.out.println("Please enter the second number to be plused.");
int number2 = sc.nextInt();
System.out.println(number1 + number2);
}
}
2)带参数的方法定义和调用
①定义
格式
public static void 方法名(参数1,参数2,…) {
}
②调用
格式
方法名(参数1,参数2,…);
注意:方法调用时,参数的数量和类型必须与方法定义中小括号里面的变量一一对应,否则程序报错。
人肉计算器2
需求:定义一个方法,在方法内部定义两个变量。
求出它们的和并打印。
import java.util.Scanner;
public class Calculator2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the first number to be plused.");
int number1 = sc.nextInt();
System.out.println("Please enter the second number to be plused.");
int number2 = sc.nextInt();
method(number1,number2);
}
public static void method(int number1,int number2) {
System.out.println(number1 + number2);
}
}
形参
全称:形式参数,是指方法定义中的参数。
实参
全称:实际参数,是指方法调用中的参数。
注意
方法调用时,参数的数量和类型(实参)必须与方法定义中小括号里面的变量(形参)一一对应,否则程序报错。
人肉计算器3
需求:定义一个方法,求长方形的周长,将结果在方法中进行打印。
import java.util.Scanner;
public class Calculator3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the length.");
double length = sc.nextDouble();
System.out.println("Please enter the width.");
double width = sc.nextDouble();
method(length,width);
}
public static void method(double length,double width) {
double perimeter = length * 2 + width *2;
System.out.println("The perimeter of rectangle is " + perimeter);
}
}
人肉计算器4
定义一个方法,求圆的面积,将结果在方法中进行打印。
import java.util.Scanner;
public class Calculator4 {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the radius of the round.");
double radius = sc.nextDouble();
method(radius);
}
publuc static void method (double r) {
double perimetre = 3.14 * r * r;
System.out.println("The perimeter of the round is " + perimeter);
}
}
3)带返回值方法的定义和调用
方法的返回值其实就是方法运行的最终结果。
如果在调用处要根据方法的结果,去编写另外的一段代码逻辑。
为了在调用处拿到方法产生的结果,就需要定义带有返回值的方法。
①定义
格式
public static 返回值类型 方法名 (参数) {
方法体;
return 返回值;
}
②调用
直接调用格式
方法名(实参);
值调用格式
数据类型 变量名 = 方法名(实参);
输出调用格式
System.out.println(方法名(实参));
人肉计算器5
import java.util.Scanner;
public class Calculator5 {
public static void main( String[] args) {
double first = firstQuarter();
double second = secondQuarter();
double third = thirdQuarter();
double fourth = fourthQuarter();
yearTurnover(first,second,third,fourth);
}
public static double firstQuarter(){
Scanner sc = new Scanner(System.in);//在需要键盘录入的方法中都要创建对象!
System.out.println("Please enter the turnover of January.");
double january = sc.nextDouble();
System.out.println("Please enter the turnover of February");
double february = sc.nextDouble();
System.out.println("Please enter the turnover of Marcy.");
double marcy = sc.nextDouble();
return january + february + marcy;
}
public static double secondQuarter(){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the turnover of April.");
double april = sc.nextDouble();
System.out.println("Please enter the turnover of May.");
double may = sc.nextDouble();
System.out.println("Please enter the turnover of June.");
double june = sc.nextDouble();
System.out.println("Please enter the turnover of July.");
return may + june + + april;
}
public static double thirdQuarter(){
Scanner sc = new Scanner(System.in);
double july = sc.nextDouble();
System.out.println("Please enter the turnover of August.");
double august = sc.nextDouble();
System.out.println("Please enter the turnover of September.");
double september = sc.nextDouble();
return july + august + september;
}
public static double fourthQuarter(){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the turnover of October.");
double octomber = sc.nextDouble();
System.out.println("Please enter the turnover of November.");
double november = sc.nextDouble();
System.out.println("Please enter the turnover of December.");
double december = sc.nextDouble();
return octomber + november + december;
}
public static void yearTurnover(double first,double second,double third,double fourth){
double year = first + second + third + fourth;
System.out.println("The yearly turnover is " + year);
}
}
比较大小
需求:定义方法,比较两个长方形的面积。
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the length of the first rectangle.");
double length1 = sc.nextDouble();
System.out.println("Please enter the width of the second rectangle.");
double width1 = sc.nextDouble();
double area1 = getArea(length1,width1);
System.out.println("Please enter the length of the second rectangle.");
double length2 = sc.nextDouble();
System.out.println("Please enter the width of the first rectangle.");
double width2 = sc.nextDouble();
double area2 = getArea(length2,width2);
if(area1 > area2){
System.out.println("The area of the first rectangle is bigger.");
} else if(area1 < area2){
System.out.println("The area of the second rectangle is bigger.");
} else{
System.out.println("The areas of the two rectangle are equal.");
}
}
public static double getArea(double length,double width){//一般把重复的或具有独立功能的代码抽取到方法中
return length * width;
}
}
4)方法的完整格式
public static 返回值类型 方法名 (参数) {
方法体;
return 返回值;
}
3.方法的注意事项
1)不调用就不执行
2)方法与方法之间是平级关系,不能互相嵌套定义
一般将main方法放在最上面
3)方法的编写顺序和执行顺序无关
4)方法的返回值类型为void,表示该方法没有返回值,return语句省略不写。如果要写return,后面不能跟具体的数据(return;->结束方法)
5)return语句下面,不能编写代码,因为永远执行不到,属于无效代码(会报错)
public class MethodDemo1{
public static void method(){
return;
System.out.println("Quit.");//❌
}
}
return
方法没有返回值:可以省略不写。如果书写,表示结束方法。
public class MethodDemo2{
public static void method(){
return;
}
}
方法有返回值:必须要写。表示结束方法和返回结果。
4.方法的重载
在同一个类中,定义了多个同名的方法,这些同名的方法具有同种的功能。
每个方法具有不同的参数类型和参数个数,这些同名的方法,就构成了重载关系。
简单记:同一个类中,方法名相同,参数不同的方法。与返回值无关。
参数不同:个数不同、类型不同、顺序不同
Java虚拟机会通过参数的不同来区分同名的方法
(1)会判断方法之间是否构成重载关系
与返回值无关
参数个数不同
参数类型不同
参数顺序不同
顺序不同可以构成重载,但是不建议!
因为可以调换顺序
要在同一个类中
(2)会定义重载的方法
数组遍历
需求:设计一个方法用于数组遍历,要求遍历的结果在一行上。例如:[11,22,33,44,55]
public class TextDemo1{
public static void main(String[] args){
int [] array = {11,22,33,44,55};
printArray(array);
}
public static void printArray(int [] array){
System.out.print("[");
for(int i = 0;i < array.length;i++){
if(i == array.length - 1){
System.out.print(array[i]);
} else {
System.out.print(array[i] + ",");
}
}
System.out.print("]");
}
}
数组最大值
需求:设计一个方法求数组的最大值,并将最大值返回
public class TextDemo2{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int [] array = {1,2,3,4,5};
int max = maxNumber(array);
System.out.println("The maximun of the array is " + max);
}
public static int maxNumber(int [] array){
int max = array[0];
for(int i = 1;i < array.length;i++){
max = array[i] > max ? array[i] : max;
}
return max;
}
}
判断是否存在
import java.util.Scanner;
public class TextDemo3{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int [] array = {1,2,3,4,5};
System.out.println("Please enter a number.");
int n = sc.nextInt();
System.out.println(compare(array,n));;
}
public static boolean compare(int [] array,int n){
for(int i = 0;i < array.length;i++){
if(array[i] == n){
return true;
}
}
return false;
}
}
return和break比较
return:其实和循环没有什么关系,跟方法有关,表示结束方法、返回结果。结束方法,里面的循环也会结束。
break:其实跟方法没有什么关系,跟结束循环或者switch有关。
复制数组
需求:定义一个方法copyOfRange(int [] arr,int from, int to)
功能:将数组arr中从索引fron(包含from)开始到索引to(不包含to)结束的元素复制到新数组中,将新数组返回。
public static int [] copyOfRange(int [] arr,int from,int to){
int [] array = new int [to - from];
for(int i = from,int j = 0;i < to;i++,j++){
array[j] = arr[i];
return array;
}
}
5.方法的内存
(1)方法调用的基本内存原理
进栈执行,方法运行进栈,运行完毕就出栈。
先进后出。
(2)方法传递基本数据类型的内存原理
基本数据类型(四类八种)
变量中存储的是真实数据
方法的值传递
传递基本数据类型时,传递的是真实的数据,形参的改变,不影响实参的值。
(3)方法传递引用数据类型的内存原理
引用数据类型
new出来的,如:数组
数据值时存储在其他空间中,变量中存储的是别的空间的地址值。
引用:使用其他空间中数据
方法的址传递
传递引用数据类型时,传递的是地址值,形参的改变,影响实参的值。