面向对象编程

面向对象编程简介

面向对象编程(OOP)是一种编程范式,它使用"对象"来组织代码和数据。Java是一种完全面向对象的编程语言,支持面向对象编程的所有核心概念。

面向对象的基本概念

// 类的定义 public class Person { // 属性 private String name; private int age; // 构造方法 public Person(String name, int age) { this.name = name; this.age = age; } // 方法 public void introduce() { System.out.println("我叫" + name + ",今年" + age + "岁。"); } }

类和对象

类是对象的模板,对象是类的实例。类定义了对象的属性和行为。

// 定义一个简单的类 public class Student { // 属性 private String name; private int studentId; private double gpa; // 构造方法 public Student(String name, int studentId) { this.name = name; this.studentId = studentId; this.gpa = 0.0; } // Getter和Setter方法 public String getName() { return name; } public void setName(String name) { this.name = name; } public int getStudentId() { return studentId; } public double getGpa() { return gpa; } public void setGpa(double gpa) { this.gpa = gpa; } // 其他方法 public void study() { System.out.println(name + "正在学习..."); } public void takeExam() { System.out.println(name + "正在考试..."); } } // 创建和使用对象 Student student1 = new Student("张三", 1001); student1.study(); student1.takeExam();

继承

继承是面向对象编程的一个重要特性,它允许一个类继承另一个类的属性和方法。

// 父类 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 + "正在吃狗粮"); } }

多态

多态是指同一个行为具有多个不同表现形式或形态的能力。

// 多态示例 public class Shape { public void draw() { System.out.println("绘制形状"); } } public class Circle extends Shape { @Override public void draw() { System.out.println("绘制圆形"); } } public class Rectangle extends Shape { @Override public void draw() { System.out.println("绘制矩形"); } } // 使用多态 Shape shape1 = new Circle(); Shape shape2 = new Rectangle(); shape1.draw(); // 输出:绘制圆形 shape2.draw(); // 输出:绘制矩形

封装

封装是将数据和方法包装在一起,隐藏内部实现细节的过程。

public class BankAccount { private String accountNumber; private double balance; private String owner; public BankAccount(String accountNumber, String owner) { this.accountNumber = accountNumber; this.owner = owner; this.balance = 0.0; } public void deposit(double amount) { if (amount > 0) { balance += amount; System.out.println("存款成功,当前余额:" + balance); } else { System.out.println("存款金额必须大于0"); } } public void withdraw(double amount) { if (amount > 0 && amount <= balance) { balance -= amount; System.out.println("取款成功,当前余额:" + balance); } else { System.out.println("取款失败,余额不足"); } } public double getBalance() { return balance; } }

抽象

抽象是隐藏实现细节,只显示功能的过程。

// 抽象类 public abstract class Vehicle { protected String brand; protected String model; public Vehicle(String brand, String model) { this.brand = brand; this.model = model; } // 抽象方法 public abstract void start(); public abstract void stop(); // 具体方法 public void displayInfo() { System.out.println("品牌:" + brand + ",型号:" + model); } } // 实现抽象类 public class Car extends Vehicle { public Car(String brand, String model) { super(brand, model); } @Override public void start() { System.out.println("汽车启动"); } @Override public void stop() { System.out.println("汽车停止"); } }

接口

接口是抽象方法的集合,它定义了一组方法,但不提供实现。

// 定义接口 public interface Drawable { void draw(); void erase(); } // 实现接口 public class Circle implements Drawable { private double radius; public Circle(double radius) { this.radius = radius; } @Override public void draw() { System.out.println("绘制圆形,半径:" + radius); } @Override public void erase() { System.out.println("擦除圆形"); } }

实践练习

练习1:图书管理系统

public class Book {
    private String title;
    private String author;
    private String isbn;
    private boolean available;
    
    // 实现图书类的基本功能
}

public class Library {
    private List books;
    
    // 实现图书馆类的基本功能
}