跳到主要内容

创建日期:2026-09-08 | 最近更新:2026-09-08 主线 JDK 11(本机实测可跑)。

Java 复习手册 2:方法与面向对象(类、封装)

你已经会变量、判断、循环了——现在学怎么把代码组织进类。这篇覆盖「方法」+「类与对象 + 封装」,是 Java 日常代码占比最高的一块。

1. 方法:先学会把逻辑装进函数

// 定义:修饰符 返回类型 方法名(参数) { 方法体 }
public static int add(int a, int b) { // public static 见下;返回 int
return a + b; // return 返回值
}

public static void greet(String name) { // void = 无返回值
System.out.println("你好," + name);
// 无 return 也行;需要提前结束可 return;
}
// 调用(static 方法同类里直接调)
int sum = add(1, 2);
greet("Lin");

// 重载:同名方法,参数不同 = 两个方法
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 void log(String... msgs) {
for (String m : msgs) System.out.println(m);
}
log("a", "b"); // 可传 0~N 个

参数传递 = 复制值:基本类型传「值副本」方法里改不影响外面;引用类型传的是「引用副本」——方法里改对象的属性会影响外面,但 重新给引用赋值不影响外面。想清楚这句,很多 bug 就没了。

2. 类与对象:图纸与实物

// 类 = 图纸(模板);对象 = 按图纸造的实物(new 出来的)
public class Student {
// 字段(属性):每个对象各有一份
String name;
int age;

// 构造器:new 时自动执行,用来初始化(名字必须和类同名,无返回值)
Student(String name, int age) {
this.name = name; // this = 「当前这个对象」,区分参数和字段
this.age = age;
}

// 方法(行为)
void sayHello() {
System.out.println("我是 " + name);
}
}
// 使用:new 造对象 → 点号访问
Student s = new Student("小明", 18); // 调用构造器
s.sayHello();
System.out.println(s.name); // 直接读字段

最容易忘

  • 构造器没有返回类型名字=类名
  • 没写构造器时,Java 给一个「无参构造器」;一旦你自己写了带参构造器,无参的就没有了(想两个都要就都写);
  • new 出来的对象在「堆」上,s 变量只是指向它的「引用」。

3. 封装:把字段藏起来,用方法访问

Java 的哲学:字段默认不让外部直接碰private),通过 public 方法读写(getter/setter)——这样能在入口加校验、后续改内部实现不破坏调用方。

public class Account {
private double balance; // 私有的,外面拿不到

public Account(double init) { this.balance = init; }

public double getBalance() { return balance; } // 读

public void deposit(double amount) { // 写,带校验
if (amount <= 0) throw new IllegalArgumentException("金额必须 > 0");
this.balance += amount;
}
}

访问修饰符(一张表背下来)

修饰符同类内同包子类全世界
private
(默认,不写)
protected
public

口诀:私有最严、公开最松;能 private 就 private

4. static:属于「类」而不是「对象」的东西

public class MathUtil {
public static final double PI = 3.14159; // 常量:static + final(大写命名)

public static int square(int x) { // static 方法:不用 new 就能调
return x * x;
}
}

// 用:类名. 直接调,不需要 new
int r = MathUtil.square(4);
double p = MathUtil.PI;
  • static 成员属于类本身,所有对象共享一份;
  • static 方法里不能直接用 this / 实例字段(还没有对象);
  • 所以程序入口 mainstatic——JVM 不用先造对象就能启动它;
  • static final = 常量。

5. 一个「什么都演示」的完整小类(看完即会)

public class Car {
private String brand; // 封装:私有字段
private int speed;

public Car(String brand) { // 构造器
this.brand = brand;
this.speed = 0;
}

public void speedUp(int delta) { this.speed += delta; }

public int getSpeed() { return this.speed; } // getter
public String getBrand() { return this.brand; }

public static void main(String[] args) { // 入口:能直接右键运行
Car c = new Car("Toyota");
c.speedUp(30);
System.out.println(c.getBrand() + " 当前 " + c.getSpeed() + " km/h");
}
}

动手 3 分钟

写一个 BankAccountprivate double balance + deposit/withdraw/getBalancewithdraw 余额不足时抛异常。

自测

  1. 构造器和普通方法区别?
  2. 自己写了带参构造器后,new Student() 还能用吗?
  3. static 方法里为什么不能直接用实例字段?
  4. this 干什么用的?
  5. 为什么推荐字段 private + 方法访问?