Java EE基础班课程(第五天)
学习目标
- 方法的概述
1.方法的概述
定义:
方法就是具有独立功能的代码块
方法的特点:
1.先创建(先定义),后使用
2.方法定义以后不会主动执行,必须要调用才指向
优点:
1,提高了代码复用性
2,提高了编码效率
2.方法的定义
格式:
/* 方法定义:
格式:
public static void 方法名(){
}
方法调用:
1.在main方法中调用;
2.方法名();
注意事项:
1.方法和方法之间是平级关系,不能嵌套定义
* 2.方法先定义,后使用
3.目前在main方法调用执行
* */
public class Demo01 {
public static void main(String[] args) {
print();
eat();
plus();
}
public static void print(){
System.out.println("HelloWorld");
}
//定义一个吃饭的方法
public static void eat(){
System.out.println("在吃饭");
}
//定义一个加法
public static void plus(){
System.out.println(1+1);
}
}
3.方法调用过程
方法没有被调用的时候,都在方法区中的字节码文件(.class)中存储
方法被调用的时候,需要进入到栈内存中运行
栈的特点:
先进后出
public class Demo03 {
public static void main(String[] args) {
eat();
}
public static void eat() {
System.out.println("吃饭");
}
}
public class Demo02方法调用过程 {
public static void main(String[] args) {
eat();
}
public static void eat() {
study();
System.out.println("吃饭");
sleep();
}
public static void sleep() {
System.out.println("睡觉");
}
public static void study() {
System.out.println("学习");
}
}
3.1方法练习
/*
需求:设计一个方法method,方法中定义一个变量(数值随意)
判断这个变量是奇数还是偶数,并在main方法中调用method。
分析:
1.功能 判断指定数字是奇数还是偶数
2.方法名:method
*/
public class Demo04 {
public static void main(String[] args) {
method();
}
public static void method(){
int a = 10;
if (a%2==0){
System.out.println(a+"是偶数");
}else {
System.out.println(a+"是奇数");
}
}
}
4.带参方法的定义和调用
/*
带参方法的定义和使用
格式:
public static void 方法名(参数){ //参数有一个,也可以有多个,多个参数要用逗号隔开
方法体
}
调用规则:
1.在main方法中调用
2.方法有参数,在调用的过程中,我们要根据参数传递数值
参数传递过程中的注意事项:
传递的参数要和方法参数的数据类型一致 (包括隐式类型转换的数据类型)
方法有几个参数,在调用过程中,我们就传递几个参数,否则报错
*/
public class Demo05 {
public static void main(String[] args) {
// method('a');
sum(1,1);
}
//判断奇偶数
public static void method(int a){
if (a%2==0){
System.out.println(a+"是偶数");
}else {
System.out.println(a+"是奇数");
}
}
//求两个数的和
public static void sum(int a,int b){
int c = a+b;
System.out.println(c);
}
}
4.1定义一个方法,求两个数的最大值,在控制台输出
/*
定义一个方法,求两个数的最大值,在控制台输出
分析:
1.给方法起名字
2.功能:求最值
3.参数:int a,int b
*/
public class Demo06 {
public static void main(String[] args) {
max(1,2);
}
//求两个数的最大值
public static void max(int a,int b){
if (a>b){
System.out.println(a);
}else {
System.out.println(b);
}
}
}
4.2定义一个方法,求n到m之间数据和,提示:默认n<m
/*
定义一个方法,求n到m之间数据和,提示:默认n<m
1.给方法起名字
2.功能:求和
3.参数列表:int n,int m
*/
public class Demo07 {
public static void main(String[] args) {
sum(1,100);
}
//定义求和方法
public static void sum(int n, int m) {
//定义求和变量
int s = 0;
//循环遍历n到m之间的数据
for (int i = n; i <= m; i++) {
s += i;
}
//输出结果
System.out.println(s);
}
}
4.3定义一个方法,求nn乘法表
/*
定义一个方法,求nn乘法表
分析:
1.给方法起名字
2.参数列表:int n
3.完成功能并调用输出结果
*/
public class Demo08 {
public static void main(String[] args) {
nn(9);
}
//nn乘法表
public static void nn(int n){
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+i*j+"\t");
}
System.out.println();
}
}
}
5.带返回值的方法的定义和调用
/*
带返回值方法定义:
格式:
public static 返回值类型 方法名(参数列表){//参数可以有一个,可以有多个,也可没有
方法体 //方法体中必须要有return语句 ,否则报错 ,return语句后面,不能有代码,应为return语句带表程序的结束
}
提示:之前我们使用 void 当做返回值类型,void就是说嘛方法没有返回值
return 关键字在方法中用来返回方法计算结果
带返回值方法调用:
1.需要几个参数,传递几个参数
2.因为方法有返回值:
可以直接调用,但是没有结果输出 不推荐使用
赋值调用, 可以接受返回的结果,推荐使用
*/
public class Demo01 {
public static void main(String[] args) {
int x = max(1, 2);
System.out.println(x);
int y = min(1, 3);
System.out.println(y);
}
//求两个数的最大值
public static int max(int a, int b) {
/*if (a > b) {
return a;
} else {
return b;
}*/
// return Math.max(a,b);
return a > b ? a : b;
}
//求两个数的最小值
/*
分析:
1.功能:求最值
2.参数列表:int a,int b
3.返回值类型:int
4.方法名: 自定义
*/
public static int min(int a, int b) {
/*if (a>b){
return b;
}else {
return a;
}*/
return a > b ? b : a;
}
}
6.方法的定义和调用小结
/*
带返回值方法定义和调用小结:
定义:
//返回值类型 :根据需要决定返回值类型,如果不需要返回值类 ,返回值类型定义为void
//参数列表:参数列表可以有一个,也可以有多个,也可以没有
//返回值数据的数据类型要和返回值类型一致
//return :
是用来返回方法运算的结果的,return后不能再写代码,否则报错(Unreachable statement),
如果方法没有返回值,可以写return语句,但是只能写return后面不能跟任何数据。
public static 返回值类型 方法名(参数列表){
方法体;
return 返回值数据;
}
调用:
有返回值方法的调用:
直接调用,不推荐
赋值调用 推荐
输出调用
没有返回值方法的调用:
直接调用 推荐
不能赋值调用
不能输出调用
*/
public class Demo02 {
public static void main(String[] args) {
//方法调用
//赋值调用 推荐
int x = max(1, 2);
System.out.println(x);
//直接调用 不推荐
max(2,3);
//输出调用
System.out.println(max(3,5));
//直接调用
printHelloWorld();
//赋值调用
// void y = printHelloWorld();
// System.out.println(y);
//输出调用
// System.out.println(printHelloWorld());
}
//求两个数的最大值
public static int max(int a, int b) {
return a > b ? a : b;
}
//打印HelloWorld
public static void printHelloWorld(){
System.out.println("HelloWorld");
return;
}
}
7.方法的注意事项
/*
方法定义的注意事项:
1.方法不能嵌套定义
2.方法的返回值类型为void,表示该方法没有返回值,
没有返回值的方法可以省略return语句不写
如果要编写return,后面不能跟具体的数据。
3.return语句下面,不能编写代码,因为永远执行不到,属于无效的代码
*/
public class Demo01 {
public static void main(String[] args) {
}
public static void sum() {
return;
}
}
8.方法的重载
/*
方法的重载:
1.在同一个类中,方法名一样
2.参数不一样:
参数类型不一样或者参数的个数不一样或者参数类型的顺序不一样
3.和返回值类型无关
*/
public class Demo01 {
public static void main(String[] args) {
int[] arr = {88, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
long[] lrr = {88, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
float[] crr = {88, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
double[] brr = {88.8, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
printArray(arr);
printArray(brr);
printArray(crr);
sum(1,1);
sum(1,1,1);
sum(1.1,1.2);
sum(1,1.1);
sum(1.1,1);
}
private static void sum(double x, int y) {
System.out.println(x+y);
}
// private static double sum(double x, int y) {
// return (x+y);
// }
private static void sum(int x, double y) {
System.out.println(x+y);
}
private static void sum(double x, double y) {
System.out.println(x+y);
}
private static void sum(int i, int j, int x) {
System.out.println(x+i+j);
}
public static void sum(int i, int j) {
System.out.println(i+j);
}
private static void printArray(float[] arr) {
System.out.print("[");
//使用for循环
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.println("]");
}
public static void printArray(double[] arr) {
System.out.print("[");
//使用for循环
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.println("]");
}
/*
分析:
1.功能:格式化打印数组
2.参数列表:int[] arr
3.返回值类型:void
*/
public static void printArray(int[] arr) {
System.out.print("[");
//使用for循环
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.println("]");
}
}
9.基本数据类型在参数传递的过程中形参的改变不影响实参
/*
形式参数:方法定义时的参数
实际参数:方法调用过程中传递的参数
基本数据类型在参数传递的过程中,形参和实参变化:
形参的改变不影响实参
*/
public class Demo01基本数据类型 {
public static void main(String[] args) {
int a= 10;
int b= 20;
System.out.println(a); // 10
System.out.println(b); // 20
changeNum(a,b);
System.out.println(a); // 10
System.out.println(b); // 20
}
public static void changeNum(int a, int b) {
System.out.println(a); // 10
System.out.println(b); // 20
a = a+b; // 30
b = a+b; // 50
System.out.println(a); // 30
System.out.println(b); // 50
}
}
10.引用数据类型
/*
引用数据类型在参数传递的过程中,形参的改变影响实参
*/
public class Demo02引用数据类型 {
public static void main(String[] args) {
//定义数组
int[] arr = {88,66,99};
System.out.println(arr);
System.out.println(arr[0]); //88
System.out.println(arr[1]); //66
System.out.println(arr[2]); //99
changeNum(arr);
System.out.println(arr);
System.out.println(arr[0]); //99
System.out.println(arr[1]); //88
System.out.println(arr[2]); //100
}
public static void changeNum(int[] arr) {
System.out.println(arr);
System.out.println(arr[0]); //88
System.out.println(arr[1]); //66
System.out.println(arr[2]); //99
arr[0]=99;
arr[1]=88;
arr[2]=100;
System.out.println(arr);
System.out.println(arr[0]); //99
System.out.println(arr[1]); //88
System.out.println(arr[2]);//100
}
}
11.定义一个方法求数组的最小值
/*
求数组最小值 ,定义方法求
分析:
1.参数列表 int[] arr
2.返回值类型 int
3.方法名 自定义
*/
public class Demo01 {
public static void main(String[] args) {
int[] arr = {88, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
//调用方法
int min = getMin(arr);
System.out.println(min);
}
public static int getMin(int[] arr){
//1.定义最小值
int min = arr[0];
//2.遍历数组 拿到每一个数据
for (int i = 0; i < arr.length; i++) {
//3.判断
if (arr[i]<min){
min = arr[i];
}
}
//返回最小值
return min;
}
}
12.定义一个方法求数组的最大值
/*
定义一个方法求数组中元素的最大值
*/
public class Demo02 {
public static void main(String[] args) {
int[] arr = {88, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
//调用方法
int max = getMax(arr);
System.out.println(max);
}
/*
分析:
1.参数列表:int[] arr
2.返回值类型:int
3.方法名
*/
public static int getMax(int[] arr){
//1.定义最大值
int max = arr[0];
//2.for循环遍历数组
for (int i = 0; i < arr.length; i++) {
//3.比较数据
if (arr[i]>max){
max = arr[i];
}
}
return max;
}
}
13.定义方法求数组中元素最大值和最小值同时返回
/*
需求:设计一个方法用于获取数组中元素的最大值,和最小值
分析: 1.定义一个数组,用静态初始化完成数组元素初始化
2.定义一个方法,用来获取数组中的最大值和最小值
2.1方法中定义一个数组,将最大值和最小值存入数组中,并将整个数组返回
2.2调用该方法,将数组中的最大值和最小值取出进行打印
分析方法的定义:
1.参数列表:
数组:int[]arr
2.功能:
求最大值和最小值 ,放到数组中,并返回
3.返回值类型:
数组: int []
*/
public class Demo03 {
public static void main(String[] args) {
//定义数组
int[] arr = {88, 66, 99, 100, 78, 99, 120, 66, 88, 99, 101};
//调用方法
int[] crr = getMaxAndMin(arr);
System.out.println("max = "+crr[0]);
System.out.println("min = "+crr[1]);
}
//求最大值和最小值 ,放到数组中,并返回
public static int[] getMaxAndMin(int [] arr){
int [] brr = new int[2];
//求最大值
int max = arr[0];
//遍历数组
for (int i = 0; i < arr.length; i++) {
if (arr[i]>max){
max = arr[i];
}
}
brr[0] = max;
//求最小值
int min = arr[0];
//遍历数组
for (int i = 0; i < arr.length; i++) {
if (arr[i]<min){
min = arr[i];
}
}
brr[1] = min;
return brr;
}
}
, 66, 88, 99, 101};
//调用方法
int[] crr = getMaxAndMin(arr);
System.out.println("max = "+crr[0]);
System.out.println("min = "+crr[1]);
}
//求最大值和最小值 ,放到数组中,并返回
public static int[] getMaxAndMin(int [] arr){
int [] brr = new int[2];
//求最大值
int max = arr[0];
//遍历数组
for (int i = 0; i < arr.length; i++) {
if (arr[i]>max){
max = arr[i];
}
}
brr[0] = max;
//求最小值
int min = arr[0];
//遍历数组
for (int i = 0; i < arr.length; i++) {
if (arr[i]<min){
min = arr[i];
}
}
brr[1] = min;
return brr;
}
}