Lesson 10
函数Functions
新格式
以前
1 2 3 4 5 public class Main { public static void main(String[] args) { // 执行代码 }}
现在
1 2 3-4+5 6 7+8+9+10 public class Main { public static void main(String[] args) { // 执行代码 new Main().run(); } public void run(){ // 执行代码 }}
功能概览
使用前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public class Question { public static void main(String[] args) { new Question().run(); } public void run(){ Console.println("请选择操作"); Console.println("1. 加法"); Console.println("2. 减法"); Console.println("3. 乘法"); Console.println("4. 除法"); Console.println("0. 退出"); int command = Console.readInt(); while (command != 0){ double value1 = scanner.nextDouble(); double value2 = scanner.nextDouble(); double result = 0.0; if (command == 1) { result = value1 + value2; } else if (command == 2) { result = value1 - value2; } else if (command == 3) { result = value1 * value2; } else if (command == 4) { result = value1 / value2; } Console.println(result); Console.println("请选择操作"); Console.println("1. 加法"); Console.println("2. 减法"); Console.println("3. 乘法"); Console.println("4. 除法"); Console.println("0. 退出"); command = Console.readInt(); } }}
功能抽离 与 转接调用
化繁为简,段落归纳
1 2 3 4 5 6 7 8*9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43+44+45+46+47+48+49+50+51 public class Question { public static void main(String[] args) { new Question().run(); } public void run(){ // 转接调用 printMenu(); int command = Console.readInt(); while (command != 0){ double value1 = scanner.nextDouble(); double value2 = scanner.nextDouble(); double result = 0.0; if (command == 1) { result = value1 + value2; } else if (command == 2) { result = value1 - value2; } else if (command == 3) { result = value1 * value2; } else if (command == 4) { result = value1 / value2; } Console.println(result); Console.println("请选择操作"); Console.println("1. 加法"); Console.println("2. 减法"); Console.println("3. 乘法"); Console.println("4. 除法"); Console.println("0. 退出"); command = Console.readInt(); } } // 功能抽离 public void printMenu(){ System.out.println("请选择操作"); System.out.println("1. 加法"); System.out.println("2. 减法"); System.out.println("3. 乘法"); System.out.println("4. 除法"); System.out.println("0. 退出"); }}
收益
将一大部分逻辑紧凑的代码进行打包
打包后 需要起个名字,可以用来总结段落大意
代码重用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33*34 35 36 37 38 39 40 41 42 43 44 45 46 47 public class Question { public static void main(String[] args) { new Question().run(); } public void run(){ // 转接调用 printMenu(); int command = Console.readInt(); while (command != 0){ double value1 = scanner.nextDouble(); double value2 = scanner.nextDouble(); double result = 0.0; if (command == 1) { result = value1 + value2; } else if (command == 2) { result = value1 - value2; } else if (command == 3) { result = value1 * value2; } else if (command == 4) { result = value1 / value2; } Console.println(result); // 代码重用 printMenu(); command = Console.readInt(); } } // 功能抽离 public void printMenu(){ System.out.println("请选择操作"); System.out.println("1. 加法"); System.out.println("2. 减法"); System.out.println("3. 乘法"); System.out.println("4. 除法"); System.out.println("0. 退出"); }}
收益
代码重用,重复代码消除
基础语法
基本元素
变量基本元素
1 2 3 4 5 6 7 8 9 10 public class Main{ public static void main(String[] args) { new Main().run(); } public void run(){ int a = 1; Console.println(a); }}
变量有 三个基本元素
声明
赋值
使用 / 取值
定义 = 声明 + 赋值
函数基本元素
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Func{ public static void main(String[] args) { new Func().run(); } public void run(){ f(); } public void f(){ System.out.println("Hello"); }}
函数也有 三个基本元素
声明
实现
使用 / 调用
定义 = 声明 + 实现
声明语法
1 2 3 public void 函数名() { <statements>}
函数声明后,上下均可使用
注意
函数的声明 应该出现在 有一个缩进的位置
调用语法
1函数名();
执行规则
遇到函数,原函数暂停
执行被调用函数
结束后,再恢复执行原函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class Func{ public static void main(String[] args) { new Func().run(); } public void run(){ 1 f(); 2 } public void f(){ 3 }}
1 2 3
挑战
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 public class Func{ public static void main(String[] args) { new Func().run(); } public void run(){ Console.println(11); h(); Console.println(12); f(); Console.println(13); } public void f(){ Console.println(21); g(); Console.println(22); h(); Console.println(23); } public void g(){ Console.println(31); h(); Console.println(32); } public void h(){ Console.println(41); }}
函数与变量作用域
通过大括号规则,可以看到 变量活在自己所在的函数里
每个函数有自己的数据空间,用于存储变量,且相互独立
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 public class Func{ public static void main(String[] args) { new Func().run(); } public void run(){ int a = 1; f(); Console.println(a); } public void f(){ int a = 1; a++; Console.println(a); }}
挑战
你会看见什么
A. 2 1 B. 2 2
函数间的数据传输
希望把数据 从一个函数里 传到 另外一个函数里
输入
数据 从 外面 进入到 函数里
A 调用 B
A 到 B
输出
数据 从 函数里 流出到 外面
A 调用 B
B 到 A
函数参数Parameters
是什么
变量,只不过可以接受传值
函数的输入
参数声明
变量定义,用 ,
分割
public void f(int a, int b) { }
参数调用
public void run() { f(3, 4); }
函数调用前表达式缩合
1 2 3 4 5 6 7 8 9 10 public void run() { int c = 3; int d = 4; int e = c + d; = 3 + 4; = 7; f(c, d); f(3, 4);}public void f(int a, int b) { int a = 3; int b = 4; Console.println(a); Console.println(b);}
传输的是值,不是变量
是数据格里的数据,而不是 数据格子
1 2 3 4 5 6 7 8 9 10 public void run() { int a = 1; int b = 2; f(a, b); f(1, 2); Console.print(a); }public void f(int a, int b) { int a = 1; int b = 2; a = 9;}
返回值Return Value
是什么
用来给 调用方 返回 结果使用
函数的输出
返回值声明
函数只能有一个返回值
1 2 3 public int f() { }
返回值
1 2 3 public int f() { return 1; }
变量接住函数返回值
函数表达式 会缩合成 函数的返回值
1 2 3 <4 public void run() { int a = f(); = 1; Console.println(a); 1}
函数返回前的表达式缩合
1 2 3 <4 5 6 7 8 9 public void run() { int a = f(); Console.println(a); 1} public int f() { int c = 1; return c; return 1;}
函数返回的是值,而不是变量
不是数据格,是数据格里的数据
1 2 3 <4 5 6 7 8 9 public void run() { int a = f(); Console.println(a); 1} public int f() { int a = 1; return a; return 1;}
小总结
完整语法
public <返回值类型> <函数名>(<函数参数列表>) { <语句> }
<函数参数列表> = <参数1类型> <参数1名>, <参数2类型> <参数2名> ...
综合应用
1 2 3 4 5 6 7 8 9 10 public void run() { int a = 1; a = f(a); Console.println(a);} public int f(int a) { a++; return a;}
函数重载
能干什么
同一个文件下,可以有同名函数
函数签名
函数签名 = 函数名 + 参数类型列表
案例
1 2 3 public int max(int a, int b) { return 0}
签名为
1max(int, int)
函数重名规则
在一个结构下,不能出现两个函数签名一样的函数
挑战
1 int f(int a){}
以下哪个函数可以跟上面的函数同时出现在一个结构里
1 2 3 4 5 int f(int a, int b){}int f(int b){}int f(double a){}int g(int a){}double f(int a){}