三大特性
面向对象编程有三大核心特性:封装、继承、多态。这三大特性是 Java 面向对象思想的基础。
三大特性概览
┌─────────────────────────────────────────────────────────────┐
│ 面向对象三大特性 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ │
│ │ 封装 │ → 数据安全、隐藏实现细节 │
│ │ Encapsu- │ │
│ │ lation │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 继承 │ → 代码复用、类层次结构 │
│ │ Inheritance │ │
│ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ │
│ │ 多态 │ → 接口复用、灵活扩展 │
│ │ Polymor- │ │
│ │ phism │ │
│ └─────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘1. 封装(Encapsulation)
概念
封装是将数据和操作数据的方法包装在一起,对外提供接口,隐藏内部实现细节。
作用
- 保护数据不被随意修改
- 隐藏复杂性
- 提供清晰的对外接口
示例
java
public class User {
// 私有属性,外部无法直接访问
private String name;
private int age;
// 公共方法,对外提供受控访问
public String getName() {
return name;
}
public void setName(String name) {
if (name == null || name.trim().isEmpty()) {
throw new IllegalArgumentException("姓名不能为空");
}
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
if (age < 0 || age > 150) {
throw new IllegalArgumentException("年龄不合法");
}
this.age = age;
}
}封装的好处
java
User user = new User();
// 不能直接访问私有属性
// user.name = "test"; // 编译错误
// 必须通过公共方法
user.setName("张三");
user.setAge(25);
// 可以添加验证逻辑
user.setAge(-10); // 抛出异常,保护数据2. 继承(Inheritance)
概念
继承是从已有类创建新类的机制,子类继承父类的属性和方法。
作用
- 代码复用
- 建立类层次结构
- 体现"is-a"关系
示例
java
// 父类
public class Animal {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(name + "正在吃东西");
}
public void sleep() {
System.out.println(name + "正在睡觉");
}
}
// 子类
public class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age); // 调用父类构造方法
this.breed = breed;
}
// 子类特有方法
public void bark() {
System.out.println(name + "汪汪叫");
}
// 重写父类方法
@Override
public void eat() {
System.out.println(name + "正在吃狗粮");
}
}继承的使用
java
Dog dog = new Dog("旺财", 3, "金毛");
// 继承自父类的方法
dog.eat(); // 输出:旺财正在吃狗粮(重写后)
dog.sleep(); // 输出:旺财正在睡觉
// 子类特有的方法
dog.bark(); // 输出:旺财汪汪叫单继承限制
Java 只支持单继承,一个类只能有一个直接父类:
java
// ❌ 错误:Java 不支持多继承
class A extends B, C { }
// ✅ 正确:多层继承
class A extends B { }
class B extends C { }3. 多态(Polymorphism)
概念
多态是指同一操作作用于不同对象,产生不同执行结果的能力。
作用
- 统一调用接口
- 增强程序灵活性
- 便于扩展
表现形式
编译时多态(方法重载)
java
public class Calculator {
// 同名方法,不同参数
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
public String add(String a, String b) {
return a + b;
}
}
// 调用时自动匹配
Calculator calc = new Calculator();
calc.add(1, 2); // int 版本
calc.add(1.1, 2.2); // double 版本
calc.add("Hello", "World"); // String 版本运行时多态(方法重写 + 向上转型)
java
// 父类引用指向子类对象
Animal animal = new Dog("旺财", 3, "金毛");
// 调用的是实际对象的方法
animal.eat(); // 输出:旺财正在吃狗粮(运行时确定)向上转型与向下转型
java
// 向上转型:父类引用指向子类对象(自动转换)
Animal animal = new Dog("旺财", 3, "金毛");
animal.eat(); // 调用 Dog 的 eat 方法
// 向下转型:父类引用转回子类引用(需要强制转换)
Dog dog = (Dog) animal;
dog.bark(); // 可以调用 Dog 特有方法
// instanceof 检查
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
}综合示例
java
public class PolymorphismDemo {
public static void main(String[] args) {
// 父类类型的数组,可以存放不同子类对象
Animal[] animals = {
new Dog("旺财", 3, "金毛"),
new Cat("咪咪", 2, "波斯"),
new Bird("小鸟", 1, "麻雀")
};
// 统一调用方法,运行时自动选择正确实现
for (Animal animal : animals) {
animal.eat(); // 每种动物吃的方式不同
}
}
}
class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public void eat() {
System.out.println(name + "正在吃东西");
}
}
class Dog extends Animal {
public Dog(String name, int age, String breed) {
super(name);
}
@Override
public void eat() {
System.out.println(name + "正在吃狗粮");
}
}
class Cat extends Animal {
public Cat(String name, int age, String breed) {
super(name);
}
@Override
public void eat() {
System.out.println(name + "正在吃猫粮");
}
}三大特性的关系
封装 → 继承 → 多态
1. 封装是基础:
- 将数据和操作封装成类
- 保护内部数据
2. 继承是扩展:
- 在封装的基础上复用代码
- 建立类层次
3. 多态是升华:
- 利用继承和重写实现
- 提供灵活的扩展能力总结
| 特性 | 关键字 | 作用 |
|---|---|---|
| 封装 | private, public, protected | 数据安全,隐藏细节 |
| 继承 | extends | 代码复用,类层次 |
| 多态 | 重写, 向上/向下转型 | 灵活扩展,统一接口 |
