Java 方法
方法是组织代码的基本单元,可以提高代码的复用性和可读性。
方法定义
基本语法
修饰符 返回类型 方法名(参数列表) {
// 方法体
return 返回值;
}
示例
public class MethodDemo {
// 无返回值方法
public static void greet() {
System.out.println("你好,欢迎学习 Java!");
}
// 有返回值方法
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
greet();
int result = add(3, 5);
System.out.println("3 + 5 = " + result);
}
}
方法参数
形式参数
// 传递基本类型
public static void printValue(int num) {
System.out.println("值:" + num);
}
// 传递引用类型
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
可变参数
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
// 调用
sum(1, 2, 3); // 6
sum(1, 2, 3, 4, 5); // 15
参数传递
// 基本类型:值传递
public static void modifyPrimitive(int num) {
num = 100; // 只修改局部变量
}
public static void main(String[] args) {
int num = 10;
modifyPrimitive(num);
System.out.println(num); // 10(不变)
}
// 引用类型:传递地址
public static void modifyArray(int[] arr) {
arr[0] = 100; // 修改数组元素
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
modifyArray(arr);
System.out.println(arr[0]); // 100(改变)
}
方法重载
方法名相同,但参数列表不同:
// 重载:参数类型不同
public static int add(int a, int b) {
return a + b;
}
public static double add(double a, double b) {
return a + b;
}
// 重载:参数个数不同
public static int add(int a, int b, int c) {
return a + b + c;
}
// 调用
add(1, 2); // 调用 add(int, int)
add(1.0, 2.0); // 调用 add(double, double)
add(1, 2, 3); // 调用 add(int, int, int)
注意:返回值类型不同不算重载
递归方法
方法调用自身:
// 阶乘
public static int factorial(int n) {
if (n <= 1) {
return 1;
}
return n * factorial(n - 1);
}
// 斐波那契数列
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
方法修饰符
访问修饰符
| 修饰符 | 同一类 | 同一包 | 子类 | 其他 |
|---|---|---|---|---|
| private | ✓ | |||
| default | ✓ | ✓ | ||
| protected | ✓ | ✓ | ✓ | |
| public | ✓ | ✓ | ✓ | ✓ |
public class ModifierDemo {
public void publicMethod() {}
protected void protectedMethod() {}
void defaultMethod() {}
private void privateMethod() {}
}
其他修饰符
// 静态方法
public static void staticMethod() {}
// final 方法(不能被重写)
public final void finalMethod() {}
// 抽象方法(没有方法体)
public abstract void abstractMethod();
静态方法
public class MathUtils {
public static int add(int a, int b) {
return a + b;
}
public static int max(int a, int b) {
return a > b ? a : b;
}
}
// 调用
int result = MathUtils.add(3, 5);
实例方法和类方法的区别
public class Person {
String name;
// 实例方法
public void speak() {
System.out.println("你好,我是" + name);
}
// 静态方法
public static void info() {
System.out.println("这是一个 Person 类");
}
}
public class Main {
public static void main(String[] args) {
// 调用实例方法
Person p = new Person();
p.name = "张三";
p.speak();
// 调用静态方法
Person.info();
}
}
构造方法
public class Person {
String name;
int age;
// 无参构造方法
public Person() {
}
// 有参构造方法
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// 创建对象
Person p1 = new Person();
Person p2 = new Person("张三", 20);
方法返回值
return 语句
public static int add(int a, int b) {
return a + b;
}
public static void print(String msg) {
System.out.println(msg);
// 无返回值,不需要 return 或 return;
}
返回数组
public static int[] getNumbers() {
return new int[]{1, 2, 3, 4, 5};
}
可变参数高级用法
public static void printAll(String... values) {
for (String value : values) {
System.out.println(value);
}
}
// 可变参数与其他参数结合
public static void print(String prefix, int... numbers) {
System.out.print(prefix);
for (int num : numbers) {
System.out.print(num + " ");
}
System.out.println();
}
小结
本章我们学习了:
- 方法的定义和调用
- 方法参数(基本类型、引用类型、可变参数)
- 参数传递(值传递和引用传递)
- 方法重载
- 递归方法
- 方法修饰符(访问修饰符、static、final、abstract)
- 静态方法和实例方法
- 构造方法
练习
- 编写一个方法,计算两个数的最大公约数
- 编写一个方法,判断一个字符串是否是回文
- 编写一个方法,接受任意数量的数字,返回最大值
- 使用递归实现汉诺塔游戏
- 创建一个矩形类,包含求面积和周长的方法