Java编程入门:从零开始掌握面向对象设计
一、Java语言基础
1.1 开发环境搭建
首先需要安装JDK(Java Development Kit),配置环境变量,并选择合适的集成开发环境,如IntelliJ IDEA或Eclipse。
1.2 基本语法结构
Java程序由类构成,每个程序至少包含一个main方法作为程序的入口点:
public class HelloWorld {
public static void main(String[] args) {
System.out.println(Hello, World!);
}
}
二、面向对象编程核心概念
2.1 类与对象
类是对象的模板,对象是类的具体实例:
// 定义学生类
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;
}
}
2.2 创建和使用对象
public class Main {
public static void main(String[] args) {
// 创建对象
Student student1 = new Student(张三, 20);
Student student2 = new Student(李四, 22);
// 调用对象方法
student1.study();
student2.study();
}
}
三、面向对象三大特性
3.1 封装
封装是将数据和行为结合在一起,并控制对数据的访问权限:
public class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accountNumber, double initialBalance) {
this.accountNumber = accountNumber;
this.balance = initialBalance;
}
// 通过公共方法访问私有属性
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public boolean withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
return true;
}
return false;
}
public double getBalance() {
return balance;
}
}
3.2 继承
继承允许创建类的层级结构,子类可以继承父类的特性:
// 父类
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 {
private String breed;
public Dog(String name, String breed) {
super(name); // 调用父类构造方法
this.breed = breed;
}
public void bark() {
System.out.println(name + 在汪汪叫);
}
}
3.3 多态
多态允许使用统一的接口处理不同类型的对象:
public class ShapeTest {
public static void main(String[] args) {
Shape[] shapes = new Shape[3];
shapes[0] = new Circle(5);
shapes[1] = new Rectangle(4, 6);
shapes[2] = new Triangle(3, 4, 5);
for (Shape shape : shapes) {
System.out.println(面积: + shape.calculateArea());
System.out.println(周长: + shape.calculatePerimeter());
}
}
}
## 四、高级面向对象概念
### 4.1 抽象类与接口
// 抽象类
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();
int getMaxAltitude();
}
// 实现接口
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 int getMaxAltitude() {
return 10000;
}
}
### 4.2 包和访问控制
- public:所有类可访问
- protected:同一包内或子类可访问
- default(包级私有):同一包内可访问
- private:仅本类可访问
## 五、设计原则与实践
### 5.1 单一职责原则
每个类应该只有一个导致变化的原因:
// 不理想的设计:一个类承担过多职责
public class StudentManager {
public void addStudent() { / ... / }
public void deleteStudent() { / ... / }
public void generateReport() { / ... / }
public void sendEmail() { / ... / }
}
// 理想的设计:分离职责
public class StudentService {
public void addStudent() { / ... / }
public void deleteStudent() { / ... / }
}
public class ReportGenerator {
public void generateReport() { / ... / }
}
public class EmailService {
public void sendEmail() { / ... / }
}
### 5.2 实际项目示例:图书馆管理系统
public class Book {
private String isbn;
private String title;
private String author;
private boolean isAvailable;
// 构造方法、getter、setter等
}
public class Library {
private List<Book> books;
public void addBook(Book book) {
books.add(book);
}
public Book findBookByTitle(String title) {
return books.stream()
.filter(book -> book.getTitle().equals(title))
.findFirst()
.orElse(null);
}
}
public class Member {
private String memberId;
private String name;
private List<Book> borrowedBooks;
public void borrowBook(Book book) {
if (book.isAvailable()) {
borrowedBooks.add(book);
book.setAvailable(false);
}
}
}
## 六、学习建议
1. 逐步深入:从基础语法开始,逐渐过渡到面向对象概念
2. 多实践:通过编写代码加深理解,完成小型项目
3. 学习优秀代码:了解开源项目的设计思路
4. 掌握设计模式:熟悉常用的面向对象设计模式
5. 持续学习:留意Java生态的发展,掌握新特性
通过系统地学习这些内容,你将能够理解Java面向对象编程的核心理念,为成为合格的Java开发者打下坚实的基础。


雷达卡


京公网安备 11010802022788号







