一、面向对象编程概述
面向对象编程(Object-Oriented Programming,简称OOP)是一种程序设计范式,它将程序中的数据和操作数据的方法封装成对象。Java是一门纯粹的面向对象语言,理解OOP的核心概念对于掌握Java至关重要。
💡 面向对象的四大特性
封装(Encapsulation)、继承(Inheritance)、多态(Polymorphism)、抽象(Abstraction)
二、类与对象
类(Class)是对象的模板,对象(Object)是类的实例。类定义了对象的属性和行为。
// 定义一个学生类
public class Student {
// 属性(成员变量)
private String name;
private int age;
private String studentId;
// 构造方法
public Student(String name, int age, String studentId) {
this.name = name;
this.age = age;
this.studentId = studentId;
}
// 方法(行为)
public void study() {
System.out.println(name + "正在学习Java");
}
public void introduce() {
System.out.println("我是" + name + ",今年" + age + "岁");
}
// Getter和Setter方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
// 创建对象并使用
public class Main {
public static void main(String[] args) {
Student student = new Student("张三", 20, "2025001");
student.introduce(); // 输出:我是张三,今年20岁
student.study(); // 输出:张三正在学习Java
}
}
三、封装
封装是将对象的属性和方法包装在一起,隐藏内部实现细节,只对外提供公共的访问方式。通过访问修饰符来控制访问权限:
private:仅本类可访问default(默认):同一包内可访问protected:同一包内及子类可访问public:所有类可访问
public class BankAccount {
private double balance; // 私有属性,外部无法直接访问
public BankAccount(double initialBalance) {
if (initialBalance > 0) {
this.balance = initialBalance;
}
}
// 通过公共方法访问私有属性
public double getBalance() {
return balance;
}
// 存款方法,包含业务逻辑验证
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println("存款成功,当前余额:" + balance);
}
}
// 取款方法,包含业务逻辑验证
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
System.out.println("取款成功,当前余额:" + balance);
} else {
System.out.println("取款失败,余额不足");
}
}
}
四、继承
继承允许一个类(子类)继承另一个类(父类)的属性和方法,实现代码复用。Java使用extends关键字实现继承。
// 父类:动物
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 + "正在吃狗粮");
}
}
// 使用
Dog dog = new Dog("旺财", 3, "金毛");
dog.eat(); // 输出:旺财正在吃狗粮(调用重写后的方法)
dog.sleep(); // 输出:旺财正在睡觉(继承自父类)
dog.bark(); // 输出:旺财汪汪叫(子类特有方法)
五、多态
多态是指同一个方法调用可以有不同的行为。多态的实现依赖于继承和方法重写。
// 定义形状接口
public interface Shape {
double getArea();
void draw();
}
// 圆形类
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public void draw() {
System.out.println("绘制圆形,半径:" + radius);
}
}
// 矩形类
public class Rectangle implements Shape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public void draw() {
System.out.println("绘制矩形,宽:" + width + ",高:" + height);
}
}
// 多态的应用
public class Main {
public static void main(String[] args) {
Shape[] shapes = new Shape[2];
shapes[0] = new Circle(5);
shapes[1] = new Rectangle(4, 6);
for (Shape shape : shapes) {
shape.draw(); // 多态:同一方法,不同行为
System.out.println("面积:" + shape.getArea());
}
}
}
六、抽象类与接口
抽象类和接口都是实现抽象的方式,但有所不同:
- 抽象类:使用
abstract关键字,可以包含抽象方法和具体方法 - 接口:使用
interface关键字,定义行为规范
// 抽象类示例
public abstract class Vehicle {
protected String brand;
public Vehicle(String brand) {
this.brand = brand;
}
// 抽象方法,子类必须实现
public abstract void start();
// 具体方法
public void stop() {
System.out.println(brand + "停止运行");
}
}
// 接口示例
public interface Flyable {
void fly();
void land();
}
// 实现抽象类和接口
public class Airplane extends Vehicle implements Flyable {
public Airplane(String brand) {
super(brand);
}
@Override
public void start() {
System.out.println(brand + "飞机启动引擎");
}
@Override
public void fly() {
System.out.println(brand + "飞机起飞");
}
@Override
public void land() {
System.out.println(brand + "飞机降落");
}
}
七、总结
面向对象编程是Java的核心,掌握封装、继承、多态和抽象这四大特性,能够帮助我们编写出结构清晰、易于维护和扩展的代码。在实际开发中,合理运用这些特性可以大大提高代码的复用性和可维护性。