一、类与对象

类是对象的模板,对象是类的实例

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 Student {
// 属性(成员变量/字段)
private String name;
private int age;

// 构造方法:创建对象时自动调用
public Student(String name, int age) {
this.name = name;
this.age = age;
}

// 方法(行为)
public void study() {
System.out.println(name + " 正在学习");
}

// getter / setter
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
}

// 使用
public class Main {
public static void main(String[] args) {
Student s1 = new Student("小明", 20); // 通过构造方法创建
s1.study(); // 调用方法
System.out.println(s1.getName()); // 通过 getter 获取
}
}

二、封装

private 保护数据,通过 public 方法对外暴露有限访问,这就是封装

修饰符 当前类 同包 子类 其他包
private
默认(无修饰符)
protected
public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class BankAccount {
private double balance; // 外部不能直接访问

public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}

public double getBalance() {
return balance;
}
// 没有 setBalance —— 不允许外部随意修改余额
}

三、继承

extends 实现继承,子类获得父类的所有属性和方法(构造方法除外)。

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
// 父类
public class Animal {
protected String name;

public Animal(String name) {
this.name = name;
}

public void eat() {
System.out.println(name + " 在吃东西");
}
}

// 子类
public class Dog extends Animal {
public Dog(String name) {
super(name); // 调用父类构造方法,必须放在第一行
}

// 子类特有方法
public void bark() {
System.out.println(name + " 汪汪叫");
}

// 重写父类方法
@Override
public void eat() {
System.out.println(name + " 在啃骨头");
}
}

super 关键字

1
2
3
4
5
6
7
public class Dog extends Animal {
public void test() {
super.eat(); // 调用父类的 eat()
this.eat(); // 调用当前类的 eat()
eat(); // 默认调用当前类的 eat()
}
}

Java 只支持单继承

一个类只能有一个直接父类,但可以通过接口实现多继承的效果。

1
2
3
4
5
// 正确
public class A extends B { }

// 错误:Java 不支持多继承
// public class A extends B, C { }

四、多态

多态的核心是:父类引用指向子类对象,调用方法时执行子类的重写版本。

1
2
3
4
5
6
7
8
9
10
11
public class Main {
public static void main(String[] args) {
Animal a1 = new Dog("旺财"); // 父类引用 → 子类对象
Animal a2 = new Cat("咪咪");

a1.eat(); // 输出:旺财 在啃骨头(执行 Dog 的 eat)
a2.eat(); // 输出:咪咪 在吃鱼(执行 Cat 的 eat)

// a1.bark(); // 编译错误!编译时看左边(Animal 没有 bark)
}
}

多态的三个条件:

  1. 继承或实现
  2. 重写父类方法
  3. 父类引用指向子类对象

五、抽象类

abstract 声明,不能实例化,可以含抽象方法(没有方法体)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public abstract class Shape {
protected String color;

// 抽象方法:子类必须重写
public abstract double area();

// 普通方法
public void show() {
System.out.println("这是一个" + color + "的图形");
}
}

public class Circle extends Shape {
private double radius;

public Circle(double r) {
this.radius = r;
}

@Override
public double area() {
return Math.PI * radius * radius;
}
}

六、接口

接口是行为的规范,用 interface 声明,用 implements 实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public interface Flyable {
void fly(); // 抽象方法(默认 public abstract)
default void land() { // 默认方法(Java 8+)
System.out.println("降落");
}
static void describe() { // 静态方法(Java 8+)
System.out.println("可以飞行的东西");
}
}

public class Bird implements Flyable {
@Override
public void fly() {
System.out.println("小鸟在飞翔");
}
}

一个类可以实现多个接口

1
2
3
public class Superman implements Flyable, Swimmable, Runnable {
// 需要实现所有接口的抽象方法
}

接口 vs 抽象类

抽象类 接口
关键字 abstract class interface
构造方法 没有
成员变量 无限制 只能是 public static final 常量
方法 可以有方法体 Java 8+ 支持 default/static 方法
继承 只能继承一个 可以实现多个

七、static 与 final

1
2
3
4
5
6
7
8
9
public class MyClass {
// static:属于类,不属于对象
static int count = 0; // 类变量
static void showCount() { } // 类方法

// final:不可修改
final int MAX = 100; // 常量
final void cannotOverride() { } // 不能被子类重写
}

本系列导航

  • (一)变量与数据类型
  • (二)运算符与流程控制
  • (三)面向对象编程基础 ← 当前