Java学习 - 黑马 - 随堂笔记 - 10_方法
1. 方法概述
1.1 什么是方法
- 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集
注意:
- 方法必须先创建才可以使用,该过程称为方法定义
- 方法创建后并不是直线运行的,需要手动使用后才执行,该过程称为方法调用
2. 方法的定义和调用
2.1 方法定义
public static void 方法名(){
//方法体
}
public static void isEvenNumber(){
//方法体
}
2.2 方法调用
- 格式:
方法名();
- 范例:
isEvenNumber();
注意:
package com.itheima_01;
public class MethodDemo {
public static void main(String[] args) {
//调用方法
isEvenNumber();
}
//需求:定义一个方法,在方法中定义一个变量,判断该数据是否是偶数
public static void isEvenNumber(){
//定义变量
int number = 10;
number = 9;
//判断该数据是否是偶数
if (number%2==0){
System.out.println(true);
}else{
System.out.println(false);
}
}
}
2.3 方法调用过程
2.4 方法练习
需求:设计一个方法用于打印两个数中的较大值
思路:
-
定义一个方法,用于打印两个数字中的较大数,例如getMax()
public static void getMax(){}
-
方法中定义两个变量,用于保存两个数字
public static void getMax(){
int a=10;
int b=20;
}
-
使用分支语句分两种情况对两个数字的大小关系进行处理
if(a>b){
System.out.println(a);
}else{
System.out.println(b);
}
-
在main()方法中调用定义好的方法
public static void main(Sting[] args){
//调用方法
getMax();
}
package com.itheima_01;
public class MethodTest {
public static void main(String[] args) {
getMax();
}
public static void getMax(){
int a=10;
int b=20;
if(a>b){
System.out.println(a);
}else {
System.out.println(b);
}
}
}
3. 带参数方法的定义和调用
3.1 带参数方法定义
- 格式:
public static void 方法名(参数){... ...}
- 格式(单个参数):
public static void 方法名(数据类型 变量名){... ...}
- 范例(单个参数):
public static void isEvenNumber(int number){... ...}
- 格式(多个参数):
public static void 方法名(数据类型 变量名1,数据类型 变量名2,......){... ...}
- 范例(多个参数):
public static void getMax(int number1,int number2){... ...}
注意:
- 方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错
- 方法定义时,多个参数之间使用逗号(,)分隔
3.2 带参数方法调用
- 格式:
方法名(参数);
- 格式(单个参数):
方法名(变量名/常量值);
- 范例(单个参数):
isEvenNumber(5);
- 格式(多个参数):
方法名(变量名1/常量值1,变量名2/常量值2);
- 范例(多个参数):
getMax(5,6);
注意:
- 方法调用时,参数的数量与类型必须与方法定义中的设置相匹配,否则程序将报错
package com.itheima_02;
public class MethodDemo {
public static void main(String[] args) {
//常量值的调用
isEvenNumber(10);
//变量的调用
int number = 8;
isEvenNumber(number);
}
//需求:定义一个方法,该方法接收一个参数,判断该数据是否是偶数
public static void isEvenNumber(int number){
if (number%2 == 0){
System.out.println(true);
}else {
System.out.println(false);
}
}
}
3.3 形参和实参
形参:方法定义中的参数
等同于变量定义格式,例如:int number
实参:方法调用中的参数
等同于使用变量或常量,例如:10 number
3.4 带参数方法练习
需求:设计一个方法用于打印两个数中的较大数,数据来自于方法参数
思路:
-
定义一个方法,用于打印两个数字中较大数,例如getMax()
public static void getMax(){}
-
为方法定义两个参数,用于接收两个数字
public static void getMax(int a,int b){}
-
使用分支语句分两种情况对两个数字的大小关系进行处理
if(a>b){
System.out.println(a);
}else{
System.out.println(b);
}
-
在main()方法中调用定义好的方法(使用常量)
public static void main(String[] args){
//直接传递常量
getMax(10,20);
}
-
在main()方法中调用定义好的方法(使用变量)
public static void main(String[] args){
//定义变量,传递
int a = 10;
int b = 20;
getMax(a,b);
}
package com.itheima_02;
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);
}
}
}
4. 带返回值方法的定义和调用
4.1 带返回值方法的定义
-
格式:
public static 数据类型 方法名(参数){
return 数据;
}
-
范例1:
public static boolean isEvenNumber(int number){
return true;
}
-
范例2:
public static int getMax(int a,int b){
return 100;
}
注意:
- 方法定义时return后面的返回值与方法定义上的数据类型要匹配,否则程序将报错
4.2 带返回值方法调用
- 格式1:方法名(参数);
- 范例:
isEvenNumber(5);
- 格式2:数据类型 变量名 = 方法名(参数)
- 范例:
boolean flag = isEvenNumber(5);
注意:
- 方法的返回值通常会使用变量接收,否则该返回值将无意义
package com.itheima_03;
public class MethodDemo {
public static void main(String[] args) {
//1.方法名(参数)
//isEvenNumber(10);
//true;无意义
//2.数据类型 变量名 = 方法名(参数);
boolean flag = isEvenNumber(10);
//boolean flag = true;
System.out.println(flag);
}
//需求:定义一个方法,该方法接收一个参数,判断该数据是否是偶数,并返回真假值
public static boolean isEvenNumber(int number){
if (number%2 == 0){
return true;
}else{
return false;
}
}
}
4.3 带返回值方法练习
需求:设计一个方法可以获取两个数的较大值,数据来自参数
思路:
-
定义一个方法,用于获取两个数字中的较大数
public static int getMax(int a,int b){}
-
使用分支语句分两种情况对两个数字的大小关系进行处理
if(a>b){}else{}
-
根据题设分别设置两种情况下对应的返回结果
if(a>b){
return a;
}else{
return b;
}
-
在main()方法中调用定义好的方法并使用变量保存
public static void main(String[] args){
int result = getMax(10,20);
System.out.println(result);
}
-
在main()方法中调用定义好的方法并直接打印结果
public static void main(String[] args){
//输出调用
System.out.println(getMax(10,20));
}
package com.itheima_03;
public class MethodTest {
public static void main(String[] args){
int result = getMax(10,20);
System.out.println(result);
System.out.println(getMax(10,20));
}
public static int getMax(int a,int b){
//使用分支语句分两种情况对两个数字的大小关系进行出来了
if(a > b){
return a;
}else{
return b;
}
}
}
5. 方法的注意事项
5.1 方法的注意事项
- void表示无返回值,可以省略return,也可以单独的书写return,后面不加数据
5.2 方法的通用格式
public static 返回值类型 方法名(参数){
方法体;
return 数据;
}
-
public static
-
返回值类型
- 方法操作完毕之后返回的数据的数据类型
- 如果方法操作完毕,没有数据返回,这里写void,而且方法体中一般不写return
-
方法名
-
参数
-
方法体
-
return
- 如果方法操作完毕,有数据返回,用于把数据返回给调用者
-
定义方法时,要做到两个明确
- 明确返回值类型:主要是明确方法操作完毕之后是否有数据返回,如果没有,写void;如果有,写对应的数据类型
- 明确参数:主要是明确参数的类型和数量
-
调用方法时
- void类型的方法,直接调用即可
- 非void类型的方法,推荐用变量接收调用
6. 方法重载
6.1 方法重载概述
方法重载指同一个类中定义的多个方法之间的关系,满足下列条件的多个方法相互构成重载
- 多个方法在同一个类中
- 多个方法具有相同的方法名
- 多个方法的参数不相同,类型不同或者数量不同
6.2 方法重载特点
- 重载仅对应方法的定义,与方法的调用无关,调用方式参照标准格式
- 重载仅针对同一个类中方法的名称与参数进行识别,与返回值无关,换句话说不能通过返回值来判定两个方法是否相互构成重载
方法重载
- 与返回值无关
- 在调用的时候,Java虚拟机会通过参数的不同来区分同名的方法
package com.itheima_05;
public class MethodDemo {
public static void main(String[] args) {
//调用方法
int result = sum(10,20);
System.out.println(result);
double result2 = sum(10.0,20.0);
System.out.println(result2);
int result3 = sum(10,20,30);
System.out.println(result3);
}
//需求1:求两个int类型数据和的方法
public static int sum(int a,int b){
return a+b;
}
//需求2:求两个double类型数据和的方法
public static double sum(double a,double b){
return a+b;
}
//需求3:求三个int类型数据和的方法
public static int sum(int a,int b,int c){
return a+b+c;
}
}
6.3 方法重载练习
需求:使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)
思路:
-
定义比较两个数字的是否相同的方法compare()方法,参数选择两个int型参数
public static boolean compare(int a,int b){
return a == b;
}
-
定义对应的重载方法,变更对应的参数类型,参数变更为两个long型参数
public static boolean compare(long a,long b){
return a == b;
}
-
定义所有的重载方法,两个byte类型与两个short类型参数
public static boolean compare(byte a,byte b){
return a == b;
}
public static boolean compare(short a,short b){
return a == b;
}
-
完成方法的调用,测试运行结果
public static void main(String[] args){
System.out.println(compare(10,20));
}
package com.itheima_05;
public class MethodTest {
public static void main(String[] args) {
//调用方法
System.out.println(compare(10,20));
System.out.println("==============");
System.out.println(compare((byte)10,(byte)20));
System.out.println("==============");
System.out.println(compare((short)10,(short)20));
System.out.println("==============");
System.out.println(compare(10L,20L));
}
//int
public static boolean compare(int a,int b){
System.out.println("int");
return a == b;
}
//long
public static boolean compare(long a,long b){
System.out.println("long");
return a == b;
}
//byte
public static boolean compare(byte a,byte b){
System.out.println("byte");
return a == b;
}
//short
public static boolean compare(short a,short b){
System.out.println("short");
return a == b;
}
}
7. 方法的参数传递
7.1 方法参数传递(基本类型)
对于基本数据类型的参数,形式参数的改变,不影响实际参数的值
package com.itheima_06;
public class ArgsDemo01 {
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;
}
}
7.2 方法参数传递(引用类型)
对于引用类型的参数,形式参数的改变,影响实际参数的值
package com.itheima_06;
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;
}
}
案例:数组遍历
需求:设计一个方法用于数组遍历,要求遍历的结果是在一行上的。例如:[11,22,33,44,55]
思路:
- 因为要求结果在一行上输出,所以这里需要再学习一个新的输出语句System.out.print(“内容”);
System.out.println("内容");
输出内容并换行System.out.print("内容");
输出内容不换行System.out.println();
起到换行的作用
- 定义一个数组,用静态初始化完成数组元素初始化
- 定义一个方法,用数组遍历通用格式对数组进行遍历
- 用新的输出语句修改遍历操作
- 调用遍历方法
package com.itheima_07;
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 i = 0; i < arr.length; i++) {
if (i == arr.length-1){
System.out.print(arr[i]);
}else{
System.out.print(arr[i]+", ");
}
}
System.out.print("]");
}
}
案例:数组最大值
需求:设计一个方法用于获取数组中元素的最大值,调用方法并输出结果
思路:
- 定义一个数组,用静态初始化完成数组元素初始化
- 定义一个方法,用来获取数组中的最大值
- 调用获取最大值方法,用变量接收返回结果
- 把结果输出在控制台
package com.itheima_07;
public class MethodTest02 {
public static void main(String[] args) {
int[] arr = {12,45,98,73,60};
int maxNumber = getMax(arr);
System.out.println("maxNumber:"+maxNumber);
}
public static int getMax(int[] arr){
int max = arr[0];
for (int x = 0; x < arr.length; x++) {
if(arr[x]>max){
max=arr[x];
}
}
return max;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)