🎨 个人介绍
👉大家好,我是:知识的搬运工旺仔
👉认真分享技术,记录学习过程的点滴,如果我的分享能为你带来帮助,请支持我奥🍻
👉你的支持,是我每天更新的动力。
👉赞点:👍 留言:✍ 收藏:⭐
👉个人格言:想法一步一步的落实,才是你我前进最佳选择。
1 )方法(method)是将具有独立功能的代码块组织称为一个整体,使其既有特殊功能的代码集
2 )注意:
public static void void 方法名( ) {
//方法体;
}
public staic void method( ) {
//方法体;
}
方法名();
methon();
方法必须先定义,后调用,否则程序将报错
总结:每个方法在被调用执行的时候,都会进入栈内存,并且拥有自己独立的内存空间,方法内部调用完毕之后,会从栈内存中弹栈消失
需求:设计一个方法用于打印两个数中的较大的数
思路:
代码:
public class MethodTest {
public static void main(String[] arr) {
//在main()方法中调用定义好的方法
getMax();
}
//定义一个方法,用于打印两个数字的最大值
public static void getMax() {
int a = 10;
int b = 20;
//使用分支语句两种情况对两个数字的大小关系进行处理
if(a > b) {
System.out.println(a);
}else {
System.out.println(b);
}
}
}
1 )定义格式:
2 )参数:由数据类型和变量名组成-- 数据类型 变量名
3 )参数实例 :int a
public static viod 方法名 (参数1) {
方法体;
}
public static void 方法名 (参数1,参数2,参数3,...) {
方法体;
}
4 )实例:
public static void isEvenNUmber(int number) {
...
}
public static void getMax(int num1, int num2) {
...
}
5 )注意:
6 )调用格式
方法名(参数);
方法名(参数1,参数2);
7 )范例
isEvenNumber(10);
getMax(10,20);
注意:调用方法时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错
1 )形参:方法定义中的参数
等同于变量定义格式,例如:int number
2 )实参:方法调用中的参数
等同于使用变量或常量,例如: 10 ,number
需求:设计一个方法用于打印两个数中较大的数,数据来自于方法参数
思路:
代码:
public class MethodTest {
public static void main(String[] args) {
//使用常量
getMax(10,20);
//使用变量
int a = 10;
int b = 20;
getMax(a,b)
}
//定义一个方法,用于打印两个数中的较大数,例如
public static void getMax(int a,int b) {
//使用分支语句两种情况对两个数字的大小关系进行处理
if(a > b) {
System.out.println(a);
} else {
System.out.println(b);
}
}
}
public static 数据类型 方法名 (参数) {
return 数据;
}
public static boolean isEvenNumber (int number) {
return true;
}
public static int getMax(int a,int b) {
return 100;
}
注意:方法定义时return 后面的返回值与方法定义上的数据类型要匹配,否则程序将报错
调用格式
调用格式
方法名(参数);
数据类型 变量 = 方法名(参数);
isEvenNumber(5);
boolean flag = isEvenNUmber(5);
注意:方法的返回值通常会使用变量接收,否则该返回值将无意义
需求:设计一个方法可以获取两个数的较大值,数据来自于参数
思路:
代码:
public class MethodTest {
public static void main(String[] ages) {
//在main()方法中调用定义好的方法并使用变量保存
int result = getMax(10,20);
System.out.println(result);
//在main()方法中调用定义好的方法直接打印
System.out.println(getMax(10,20));
}
public static int getMax(int a,int b) {
//使用分支语句,进行判断
if(a > b) {
return a;
}else {
return b;
}
}
}
1 )方法不能嵌套定义
public class MethodDemo {
public static void main(String[] args) {
}
public static void methodone () {
public static void methodTwo() {
//这里会编译报错
}
}
}
2 )void表示五返回值,可以省略return,也可以单独的书写不加数据
public class MethodDemo {
public static void methodTwo() {
return;
}
}
1 )格式:
public static 返回值类型 方法名(参数) {
方法体;
return 数据;
}
2 )解释:
方法名 调用方法时候使用的标识
参数 有数据类型和变量名组成,多个参数之间用逗号隔开
方法体, 完成功能的代码块
return 如果方法操作完毕,有数据返回,用于把数据返回给调用者
3 )定义方法时,要做到两个明确
4 )调用方法时的注意:
1 )方法重载概念
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
2 )注意:
3 )正确实例:
public class MethodDemo {
public static void fn(int a) {
//方法体
}
public static int (double a) {
//方法体
}
}
public class MethodDemo {
public static float (int a ) {
//方法体
}
public static int fn(int a,int b) {
//方法体
}
}
4 )错误实例
public class MethodDemo {
public static void fn(int a) {
//方法体
}
public static int (int a) {
//方法体
}
}
public class MethodDemo1 {
public static void fn(int a) {
//方法体
}
}
public class MethodDemo2 { // 错误是因为在两个类里边了
public static int fn(double a) {
//方法体
}
}
需求:使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)
思路:
代码:
public class MethodTest {
public static void main(String[] args) {
//调用方法
System.out.println(compare(10,20));
System.out.println(compare((byte)10,(byte)20));
System.out.println(compare((short)10,(short)20));
System.out.println(compare(10L,20L));
}
//int
public static boolean compare(int a, int b) {
System.out.prinln("int");
return a==b;
}
//byte
public static boolean compare(byte a, byte b) {
System.out.prinln("byte");
return a==b;
}
//short
public static boolean compare(short a, short b) {
System.out.prinln("short ");
return a==b;
}
//long
public static boolean compare(long a, long b) {
System.out.prinln("long ");
return a==b;
}
}
1 )测试代码
public class ArgsDemo {
public static void main(String[] args) {
int number = 100;
System.out.println("调用change方法前" + number);
change(number);
System.out.println("调用change方法后:"+ number);
}
public static void change(int number) {
number = 200;
}
}
2 )结论:
3 )结论依据:
1 )测试代码
public class ArgsDemo02 {
public static void main(String[] args) {
int[] arr = {10,20,30};
System.out.println("调用change方法前:" + arr[1]);
change(arr);
System.out.println("调用方法change方法后:" + arr[1])
}
public static void change(int[] arr) {
arr[1] = 200;
}
}
2 )结论:
对于引用类型的参数,形式参数的改变,影响实际参数的值
3 )结论依据:
引用数据类型的传参,传入的是地址值,内存中会造成两个引用的指向同一个内存的效果,所以即使方法弹栈,堆内存中的数据也已经是改变后的结果
需求:设计一个方法用于数组遍历,要求遍历的结果是在一行的,例如:[11,22,33,44,55]
思路:
因为要求结果在一行输出,所以这里需要在学习一个新的输出语句System.out.prin(“内容”);
定义一个数组,用静态初始化完成数组元素初始化
定义一个方法,用数组遍历通用格式对数组进行遍历
用新的输出语句修改遍历操作
调用遍历方法
代码:
public class MethodTest01 {
public static void main(String[] args) {
//定义一个数组,用静态初始化完成数组元素初始化
int[] arr = {11,22,33,44,55};
//调用方法
printArray(arr);
}
public static void printArray(int[] arr) {
System.out.print("[");
for(int x = 0; x< arr.length;x++) {
if(x == arr.length-1) {
System.out.print(arr[x]);
}else {
System.out.print(arr[x]+",")
}
}
System.out.print("]");
}
}
需求:设计一个方法用于获取数组中元素的最大值
思路:
代码:
public class MethodTest02 {
public static void main(String[] args) {
//定义一个数组,用静态初始化完成数组元素初始化
int[] arr = {12,45,98,73,60};
//调用获取最大值方法,用变量接收返回结果
int number = getMax(arr);
//把结果输出在控制台
System.out.println("number :" + number);
}
//定义一个方法,用来获取数组中的最大值
public static int getMax(int[] arr) {
int max = arr[0];
for(int x=1;x