方法的概述及基本使用
方法的概述
方法就是完成特定功能的代码块,在很多语言里面都有函数的定义 , 函数在Java中被称为方法。
方法格式
修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2…) {
函数体;
return 返回值;
}
方法格式解释
修饰符 目前记住 public static
返回值类型 用于限定返回值的数据类型
方法名 一个名字,为了方便我们调用方法
参数类型 用于接收调用方法时传入的数据的类型
参数名 用于接收调用方法时传入的数据的变量
方法体 完成功能的代码
return 结束方法,把返回值带给调用者
求和方法的编写
public class MethodDemo {
/*
* 写一个方法,用于求和。 两个明确: 返回值类型 int 参数列表 int a,int b
*/
public static int sum(int a, int b) {
// int c = a + b;
// return c;
return a + b;
}
public static void main(String[] args) {
}
}
求和方法的调用
有明确返回值的方法调用:
单独调用,没有意义
输出调用,有意义,但是不够好,因为我不一定非要把结果输出
赋值调用,推荐方式
public class MethodDemo2 {
// 求和的方法
public static int sum(int a, int b) {
return a + b;
}
public static void main(String[] args) {
// 单独调用
// sum(10,20);
// 输出调用
// System.out.println(sum(10,20));
// 赋值调用
int s = sum(10, 20);
// s+=100;
System.out.println("s:"+s);
}
}
求和方法的调用图解
方法的练习及注意事项
获取两个数据中的较大值
import java.util.Scanner;
/*
* 需求:键盘录入两个数据,返回两个数中的较大值
*
* 两个明确:
* 返回值类型:int
* 参数列表:int a,int b
*/
public class MethodTest {
// 返回两个数中的较大值
public static int getMax(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
System.out.println("请输入第一个数据:");
int x = sc.nextInt();
System.out.println("请输入第二个数据:");
int y = sc.nextInt();
//调用方法
int max = getMax(x,y);
System.out.println("max:"+max);
}
}
比较两个数据是否相等
import java.util.Scanner;
/*
* 需求:键盘录入两个数据,比较两个数是否相等
*
* 两个明确:
* 返回值类型:boolean
* 参数列表:int a,int b
*/
public class MethodTest2 {
//比较两个数是否相等
public static boolean compare(int a,int b){
if(a==b){
return true;
}else {
return false;
}
}
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
System.out.println("请输入第一个数据:");
int a = sc.nextInt();
System.out.println("请输入第二个数据:");
int b = sc.nextInt();
//调用方法
boolean flag = compare(a,b);
System.out.println("flag:"+flag);
}
}
获取三个数据中的较大值
import java.util.Scanner;
/*
* 需求:键盘录入三个数据,返回三个数中的最大值
*
* 两个明确:
* 返回值类型:int
* 参数列表:int a,int b,int c
*/
public class MethodTest3 {
// 返回三个数中的最大值
public static int getMax(int a, int b, int c) {
if (a > b) {
if (a > c) {
return a;
} else {
return c;
}
} else {
if (b > c) {
return b;
} else {
return c;
}
}
}
public static void main(String[] args) {
//创建对象
Scanner sc = new Scanner(System.in);
//接收数据
System.out.println("请输入第一个数据:");
int a = sc.nextInt();
System.out.println("请输入第二个数据:");
int b = sc.nextInt();
System.out.println("请输入第三个数据:");
int c = sc.nextInt();
//调用方法
int max = getMax(a,b,c);
System.out.println("max:"+max);
}
}
void修饰的方法的调用
public class MethodDemo {
//在控制台输出10次HelloWorld案例。
public static void printHelloWorld() {
for(int x=1; x<=10; x++) {
System.out.println("HelloWorld");
}
}
public static void main(String[] args) {
//单独调用
printHelloWorld();
打印1-n之间的数据
public class MethodTest {
//在控制台打印1到该数据n的值
public static void printNumber(int n) {
for(int x=1; x<=n; x++) {
System.out.println(x);
}
}
public static void main(String[] args) {
printNumber(10);
System.out.println("-------------------");
printNumber(100);
}
}
方法的重载及参数的传递
方法重载的概述和基本使用
在同一个类中,允许存在一个以上的同名方法,只要它们的参数个数或者参数类型不同即可。
方法重载特点
与返回值类型无关,只看方法名和参数列表
在调用时,虚拟机通过参数列表的不同来区分同名方法
public class MethodDemo {
public static void main(String[] args) {
int a = 10;
int b = 20;
// 求和
int result = sum(a, b);
System.out.println("result:" + result);
int c = 30;
// 求和
//int result2 = sum2(a,b,c);
//System.out.println("result2:"+result2);
result = sum(a,b,c);
System.out.println("result:"+result);
}
//两个float类型的数据求和
public static float sum(float a,float b) {
return a + b;
}
// 三个整数的求和
public static int sum(int a,int b,int c) {
return a + b + c;
}
/*
public static int sum2(int a, int b, int c) {
return a + b + c;
}
*/
// 两个整数的求和
public static int sum(int a, int b) {
return a + b;
}
}
方法中参数的传递
方法的参数是基本类型的时候:
形式参数的改变不影响实际参数。
形式参数:用于接收实际数据的变量
实际参数:实际参与运算的变量
方法的参数是引用数据类型的时候:
形式参数的改变直接影响实际参数
方法的操作数组的练习
把遍历数组改进为方法实现,并调用方法
public class MethodTest {
public static void main(String[] args) {
// 定义数组
int[] arr = { 11, 22, 33, 44, 55 };
// 遍历
// for (int x = 0; x < arr.length; x++) {
// System.out.println(arr[x]);
// }
//用方法改进
//printArray(arr);
//这一次虽然可以,但是我觉得格式不好看,能不能打印成下面的格式呢?
//[元素1, 元素2, 元素3, ...]
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.println(arr[x]+"]");
}else {
System.out.print(arr[x]+", ");
}
}
}
把获取数组最值改进为方法实现,并调用方法
public class MethodTest2 {
public static void main(String[] args) {
// 定义数组
int[] arr = { 34, 67, 10, 28, 59 };
//获取数组中的最大值
// //定义参照物
// int max = arr[0];
// //遍历,依次比较,大的留下来
// for(int x=1; x<arr.length; x++) {
// if(arr[x] > max) {
// max = arr[x];
// }
// }
//用方法改进
int max = getMax(arr);
System.out.println("max:"+max);
//获取数组中的最小值,用方法实现
int min = getMin(arr);
System.out.println("min:"+min);
}
//获取数组中的最小值的方法
public static int getMin(int[] arr) {
int min = arr[0];
for(int x=1; x<arr.length; x++) {
if(arr[x] < min) {
min = arr[x];
}
}
return min;
}
写一个方法,用于对数组进行求和,并调用方法
public class MethodTest3 {
public static void main(String[] args) {
// 定义数组
int[] arr = { 1, 2, 3, 4, 5 };
// //定义求和变量
// int sum = 0;
// //获取数组中的每一个元素
// for(int x=0; x<arr.length; x++) {
// sum += arr[x];
// }
//用方法改进
int sum = sum(arr);
System.out.println("sum:"+sum);
}
public static int sum(int[] arr) {
int sum = 0;
for(int x=0; x<arr.length; x++) {
sum += arr[x];
}
return sum;
}
}