第7章 方法:把复杂程序拆成小块
到目前为止,我们已经能写输入、输出、条件、循环。问题是,如果所有逻辑都写在 main 方法里,代码会越来越长。
比如一个注册程序:
public class RegisterDemo {
public static void main(String[] args) {
String phone = "13800000000";
String password = "123456";
int age = 20;
if (phone == null || phone.length() != 11 || !phone.startsWith("1")) {
System.out.println("手机号不合法");
return;
}
if (password == null || password.length() < 6) {
System.out.println("密码不合法");
return;
}
if (age < 18) {
System.out.println("未满18岁");
return;
}
System.out.println("注册成功");
}
}
现在还能读。规则再多十条,main 就会变成一大坨。方法就是用来拆分逻辑的。
一、方法是什么
方法是一段有名字的代码。
public static void sayHello() {
System.out.println("Hello");
}
调用:
sayHello();
完整程序:
public class MethodDemo {
public static void main(String[] args) {
sayHello();
sayHello();
}
public static void sayHello() {
System.out.println("Hello");
}
}
输出:
Hello
Hello
方法的价值:
- 避免重复。
- 给逻辑起名字。
- 降低主流程复杂度。
- 让代码更容易测试。
二、方法的结构
public static void sayHello() {
System.out.println("Hello");
}
可以拆成:
public static 返回值类型 方法名(参数列表) {
方法体
}
第一阶段我们先统一使用 public static。
public 和 static 后面面向对象阶段再深入。
现在重点看:
- 返回值类型。
- 方法名。
- 参数列表。
- 方法体。
三、参数:方法的输入
没有参数的方法只能做固定事情:
public static void printTom() {
System.out.println("Tom");
}
有参数的方法更灵活:
public static void printName(String name) {
System.out.println("姓名:" + name);
}
调用:
printName("Tom");
printName("Jack");
多个参数:
public static void printUser(String name, int age) {
System.out.println("姓名:" + name);
System.out.println("年龄:" + age);
}
参数是调用方传给方法的数据。方法不要总是依赖写死的数据。
四、返回值:方法的输出
没有返回值用 void:
public static void printLine() {
System.out.println("——");
}
有返回值:
public static int add(int a, int b) {
return a + b;
}
调用:
int result = add(3, 5);
System.out.println(result);
返回值让方法可以参与后续计算,而不是只能打印。
不推荐把计算和打印绑死:
public static void addAndPrint(int a, int b) {
System.out.println(a + b);
}
更好:
public static int add(int a, int b) {
return a + b;
}
外层决定怎么输出。
五、return 的两个作用
1. 返回结果
public static int max(int a, int b) {
if (a >= b) {
return a;
}
return b;
}
2. 提前结束方法
public static void checkAge(int age) {
if (age < 18) {
System.out.println("未满18岁");
return;
}
System.out.println("可以注册");
}
return 后,方法立即结束。
六、方法命名
方法名要表达动作。
好名字:
- isValidPhone
- calculateTotalPrice
- printMenu
- readScore
- findMaxScore
差名字:
- doIt
- handle
- aaa
- test
布尔返回值方法常用:
public static boolean isAdult(int age) {
return age >= 18;
}
public static boolean hasPermission(boolean login, boolean granted) {
return login && granted;
}
读起来像问题:
- 是否成年?
- 是否有权限?
七、方法重载
同名方法,参数不同,叫重载。
public static int add(int a, int b) {
return a + b;
}
public static int add(int a, int b, int c) {
return a + b + c;
}
public static double add(double a, double b) {
return a + b;
}
调用时,编译器根据参数选择方法。
System.out.println(add(1, 2));
System.out.println(add(1, 2, 3));
System.out.println(add(1.5, 2.5));
重载适合同一语义、不同参数形式。
不要用重载表达完全不同的事。
八、作用域
方法里的变量只在方法内部有效。
public static void test() {
int count = 10;
}
public static void main(String[] args) {
// System.out.println(count); // 错误
}
代码块里的变量也只在块内有效:
if (true) {
int value = 100;
}
// System.out.println(value); // 错误
变量作用域越小,代码越容易理解。不要把变量提前定义到很大范围。
九、基本类型参数和引用类型参数
基本类型:
public class ParamDemo {
public static void main(String[] args) {
int age = 18;
change(age);
System.out.println(age);
}
public static void change(int value) {
value = 20;
}
}
输出:
18
方法里改的是副本,不影响外面的 age。
数组是引用类型:
public class ArrayParamDemo {
public static void main(String[] args) {
int[] scores = {60, 70};
change(scores);
System.out.println(scores[0]);
}
public static void change(int[] values) {
values[0] = 100;
}
}
输出:
100
因为方法内外指向同一个数组对象。
第一阶段先记住:基本类型传进去不会改外部变量,数组这类引用类型修改内容会影响外部对象。
十、把注册逻辑拆成方法
public class RegisterMethodDemo {
public static void main(String[] args) {
String phone = "13800000000";
String password = "123456";
int age = 20;
if (!isValidPhone(phone)) {
System.out.println("手机号不合法");
return;
}
if (!isValidPassword(password)) {
System.out.println("密码不合法");
return;
}
if (!isAdult(age)) {
System.out.println("未满18岁");
return;
}
System.out.println("注册成功");
}
public static boolean isValidPhone(String phone) {
return phone != null && phone.length() == 11 && phone.startsWith("1");
}
public static boolean isValidPassword(String password) {
return password != null && password.length() >= 6;
}
public static boolean isAdult(int age) {
return age >= 18;
}
}
主流程现在很清楚:
校验手机号
-> 校验密码
-> 校验年龄
-> 注册成功
每个规则单独放在方法里,后面修改也更方便。
十一、方法的单一职责
一个方法最好只做一件事。
不推荐:
public static void validateAndSaveAndPrint(String phone) {
}
这个名字已经说明它做太多事:校验、保存、打印。
更好:
public static boolean isValidPhone(String phone) {}
public static void savePhone(String phone) {}
public static void printSuccess() {}
单一职责的好处:
- 容易命名。
- 容易测试。
- 容易复用。
- 容易排查。
如果一个方法很难起名,通常说明它职责不清。
十二、方法实战:成绩统计拆分
public class ScoreMethodDemo {
public static void main(String[] args) {
int[] scores = {90, 85, 70, 60};
System.out.println("最高分:" + findMax(scores));
System.out.println("最低分:" + findMin(scores));
System.out.println("平均分:" + calculateAverage(scores));
}
public static int findMax(int[] scores) {
int max = scores[0];
for (int score : scores) {
if (score > max) {
max = score;
}
}
return max;
}
public static int findMin(int[] scores) {
int min = scores[0];
for (int score : scores) {
if (score < min) {
min = score;
}
}
return min;
}
public static double calculateAverage(int[] scores) {
int sum = 0;
for (int score : scores) {
sum += score;
}
return sum * 1.0 / scores.length;
}
}
这里还有一个问题:如果 scores 是空数组,会出错。更严谨的版本要加校验。阶段项目里会处理。
十三、常见错误
1. 有返回值却忘记 return
public static int add(int a, int b) {
int result = a + b;
}
声明返回 int,就必须 return int。
2. return 类型不匹配
public static int getAge() {
return "18";
}
字符串不能作为 int 返回。
3. 参数顺序传错
calculatePrice(count, unitPrice);
如果两个参数都是 int,编译器可能发现不了语义错误。方法名、参数名要清楚,参数太多时后面要用对象封装。
4. 方法太长
一个方法超过几十行,就要考虑能不能拆。
5. 方法名和行为不一致
isValidPhone 里不要保存数据。方法名承诺了它只是校验。
十四、本章练习
写方法 isEven(int number),判断偶数。
写方法 max(int a, int b),返回较大值。
写方法 calculateBmi(double height, double weight)。
写方法 getScoreLevel(int score),返回优秀、良好、及格、不及格、非法。
把注册校验拆成三个方法:手机号、密码、年龄。
给成绩数组写三个方法:最高分、最低分、平均分。
十五、本章总结
方法是组织代码的第一把工具。
你需要掌握:
- 方法是一段有名字的代码。
- 参数是输入。
- 返回值是输出。
- void 表示没有返回值。
- return 可以返回结果,也可以结束方法。
- 方法名要表达意图。
- 方法重载适合同一语义的不同参数。
- 变量有作用域。
- 基本类型和引用类型作为参数表现不同。
- 方法要尽量单一职责。
下一章是第一阶段项目。我们会把输入、条件、循环、方法组合起来,写一个命令行成绩统计器。





