Initial commit
This commit is contained in:
115
docs/java/basic.md
Normal file
115
docs/java/basic.md
Normal file
@@ -0,0 +1,115 @@
|
||||
# Java基础语法
|
||||
|
||||
## 变量与数据类型
|
||||
|
||||
Java是一种强类型语言,每个变量都必须声明其类型。
|
||||
|
||||
### 基本数据类型
|
||||
|
||||
Java有8种基本数据类型:
|
||||
|
||||
| 类型 | 关键字 | 大小 | 取值范围 |
|
||||
|------|--------|------|----------|
|
||||
| 字节型 | byte | 1字节 | -128 ~ 127 |
|
||||
| 短整型 | short | 2字节 | -32768 ~ 32767 |
|
||||
| 整型 | int | 4字节 | -2^31 ~ 2^31-1 |
|
||||
| 长整型 | long | 8字节 | -2^63 ~ 2^63-1 |
|
||||
| 单精度浮点 | float | 4字节 | 约±3.4E38 |
|
||||
| 双精度浮点 | double | 8字节 | 约±1.7E308 |
|
||||
| 字符型 | char | 2字节 | 0 ~ 65535 |
|
||||
| 布尔型 | boolean | 1位 | true/false |
|
||||
|
||||
### 变量声明
|
||||
|
||||
```java
|
||||
// 声明变量
|
||||
int age = 25;
|
||||
String name = "张三";
|
||||
double salary = 10000.50;
|
||||
boolean isActive = true;
|
||||
|
||||
// 常量
|
||||
final double PI = 3.14159;
|
||||
```
|
||||
|
||||
## 运算符
|
||||
|
||||
### 算术运算符
|
||||
|
||||
```java
|
||||
int a = 10, b = 3;
|
||||
System.out.println(a + b); // 13 加法
|
||||
System.out.println(a - b); // 7 减法
|
||||
System.out.println(a * b); // 30 乘法
|
||||
System.out.println(a / b); // 3 除法
|
||||
System.out.println(a % b); // 1 取余
|
||||
```
|
||||
|
||||
### 比较运算符
|
||||
|
||||
```java
|
||||
int x = 5, y = 10;
|
||||
System.out.println(x == y); // false
|
||||
System.out.println(x != y); // true
|
||||
System.out.println(x > y); // false
|
||||
System.out.println(x < y); // true
|
||||
```
|
||||
|
||||
## 流程控制
|
||||
|
||||
### 条件语句
|
||||
|
||||
```java
|
||||
int score = 85;
|
||||
|
||||
if (score >= 90) {
|
||||
System.out.println("优秀");
|
||||
} else if (score >= 60) {
|
||||
System.out.println("及格");
|
||||
} else {
|
||||
System.out.println("不及格");
|
||||
}
|
||||
```
|
||||
|
||||
### 循环语句
|
||||
|
||||
```java
|
||||
// for循环
|
||||
for (int i = 0; i < 5; i++) {
|
||||
System.out.println(i);
|
||||
}
|
||||
|
||||
// while循环
|
||||
int i = 0;
|
||||
while (i < 5) {
|
||||
System.out.println(i);
|
||||
i++;
|
||||
}
|
||||
|
||||
// 增强for循环
|
||||
int[] arr = {1, 2, 3, 4, 5};
|
||||
for (int num : arr) {
|
||||
System.out.println(num);
|
||||
}
|
||||
```
|
||||
|
||||
## 数组
|
||||
|
||||
```java
|
||||
// 声明数组
|
||||
int[] numbers = new int[5];
|
||||
String[] names = {"张三", "李四", "王五"};
|
||||
|
||||
// 访问数组元素
|
||||
numbers[0] = 10;
|
||||
System.out.println(names[1]); // 李四
|
||||
|
||||
// 数组长度
|
||||
System.out.println(names.length); // 3
|
||||
```
|
||||
|
||||
## 验证自动化部署
|
||||
|
||||
> 2026-05-07: 此文档已通过 CI/CD 自动化部署验证
|
||||
>
|
||||
> 第四次验证 - webhook 已改为内网地址
|
||||
175
docs/java/collection.md
Normal file
175
docs/java/collection.md
Normal file
@@ -0,0 +1,175 @@
|
||||
# 集合框架
|
||||
|
||||
## 集合体系结构
|
||||
|
||||
Java集合框架主要分为两大类:
|
||||
|
||||
1. **Collection接口**:存储单值
|
||||
- List:有序、可重复
|
||||
- Set:无序、不可重复
|
||||
- Queue:队列
|
||||
|
||||
2. **Map接口**:存储键值对
|
||||
|
||||
## List集合
|
||||
|
||||
### ArrayList
|
||||
|
||||
基于动态数组实现,查询快,增删慢。
|
||||
|
||||
```java
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
|
||||
// 添加元素
|
||||
list.add("Java");
|
||||
list.add("Python");
|
||||
list.add("JavaScript");
|
||||
|
||||
// 获取元素
|
||||
System.out.println(list.get(0)); // Java
|
||||
|
||||
// 遍历
|
||||
for (String item : list) {
|
||||
System.out.println(item);
|
||||
}
|
||||
|
||||
// 删除元素
|
||||
list.remove("Python");
|
||||
|
||||
// 集合大小
|
||||
System.out.println(list.size()); // 2
|
||||
```
|
||||
|
||||
### LinkedList
|
||||
|
||||
基于链表实现,增删快,查询慢。
|
||||
|
||||
```java
|
||||
import java.util.LinkedList;
|
||||
|
||||
LinkedList<String> linkedList = new LinkedList<>();
|
||||
|
||||
linkedList.addFirst("第一");
|
||||
linkedList.addLast("最后");
|
||||
linkedList.add("中间");
|
||||
|
||||
System.out.println(linkedList.getFirst()); // 第一
|
||||
System.out.println(linkedList.getLast()); // 最后
|
||||
```
|
||||
|
||||
## Set集合
|
||||
|
||||
### HashSet
|
||||
|
||||
基于哈希表实现,无序、不可重复。
|
||||
|
||||
```java
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
Set<String> set = new HashSet<>();
|
||||
|
||||
set.add("Apple");
|
||||
set.add("Banana");
|
||||
set.add("Apple"); // 重复元素不会被添加
|
||||
|
||||
System.out.println(set.size()); // 2
|
||||
System.out.println(set.contains("Apple")); // true
|
||||
```
|
||||
|
||||
### TreeSet
|
||||
|
||||
基于红黑树实现,自动排序。
|
||||
|
||||
```java
|
||||
import java.util.TreeSet;
|
||||
|
||||
Set<Integer> treeSet = new TreeSet<>();
|
||||
|
||||
treeSet.add(5);
|
||||
treeSet.add(1);
|
||||
treeSet.add(3);
|
||||
|
||||
// 输出:1, 3, 5(自动排序)
|
||||
for (Integer num : treeSet) {
|
||||
System.out.println(num);
|
||||
}
|
||||
```
|
||||
|
||||
## Map集合
|
||||
|
||||
### HashMap
|
||||
|
||||
基于哈希表实现,键不可重复。
|
||||
|
||||
```java
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
Map<String, Integer> map = new HashMap<>();
|
||||
|
||||
// 添加键值对
|
||||
map.put("Java", 1);
|
||||
map.put("Python", 2);
|
||||
map.put("JavaScript", 3);
|
||||
|
||||
// 获取值
|
||||
System.out.println(map.get("Java")); // 1
|
||||
|
||||
// 遍历
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
System.out.println(entry.getKey() + ": " + entry.getValue());
|
||||
}
|
||||
|
||||
// 判断键是否存在
|
||||
System.out.println(map.containsKey("Java")); // true
|
||||
|
||||
// 删除
|
||||
map.remove("Python");
|
||||
```
|
||||
|
||||
### TreeMap
|
||||
|
||||
基于红黑树实现,键自动排序。
|
||||
|
||||
```java
|
||||
import java.util.TreeMap;
|
||||
|
||||
Map<Integer, String> treeMap = new TreeMap<>();
|
||||
|
||||
treeMap.put(3, "C");
|
||||
treeMap.put(1, "A");
|
||||
treeMap.put(2, "B");
|
||||
|
||||
// 按键排序输出:1=A, 2=B, 3=C
|
||||
treeMap.forEach((k, v) -> System.out.println(k + "=" + v));
|
||||
```
|
||||
|
||||
## 集合工具类
|
||||
|
||||
```java
|
||||
import java.util.Collections;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
List<Integer> numbers = new ArrayList<>();
|
||||
numbers.add(3);
|
||||
numbers.add(1);
|
||||
numbers.add(2);
|
||||
|
||||
// 排序
|
||||
Collections.sort(numbers); // [1, 2, 3]
|
||||
|
||||
// 反转
|
||||
Collections.reverse(numbers); // [3, 2, 1]
|
||||
|
||||
// 打乱
|
||||
Collections.shuffle(numbers);
|
||||
|
||||
// 最大最小值
|
||||
System.out.println(Collections.max(numbers));
|
||||
System.out.println(Collections.min(numbers));
|
||||
```
|
||||
10
docs/java/index.md
Normal file
10
docs/java/index.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# Java 笔记
|
||||
|
||||
Java技术栈学习笔记,涵盖基础语法、面向对象、集合框架、多线程等内容。
|
||||
|
||||
## 目录
|
||||
|
||||
- [Java基础语法](/java/basic.html)
|
||||
- [面向对象编程](/java/oop.html)
|
||||
- [集合框架](/java/collection.html)
|
||||
- [多线程编程](/java/thread.html)
|
||||
157
docs/java/oop.md
Normal file
157
docs/java/oop.md
Normal file
@@ -0,0 +1,157 @@
|
||||
# 面向对象编程
|
||||
|
||||
## 类与对象
|
||||
|
||||
### 类的定义
|
||||
|
||||
```java
|
||||
public class Person {
|
||||
// 属性(字段)
|
||||
private String name;
|
||||
private int age;
|
||||
|
||||
// 构造方法
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
// 方法
|
||||
public void sayHello() {
|
||||
System.out.println("你好,我是" + name);
|
||||
}
|
||||
|
||||
// Getter和Setter
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 创建对象
|
||||
|
||||
```java
|
||||
Person person = new Person("张三", 25);
|
||||
person.sayHello(); // 你好,我是张三
|
||||
person.setName("李四");
|
||||
System.out.println(person.getName()); // 李四
|
||||
```
|
||||
|
||||
## 封装
|
||||
|
||||
封装是面向对象的三大特性之一,通过访问修饰符控制类的成员访问权限。
|
||||
|
||||
| 修饰符 | 同一类 | 同一包 | 子类 | 其他 |
|
||||
|--------|--------|--------|------|------|
|
||||
| public | ✓ | ✓ | ✓ | ✓ |
|
||||
| protected | ✓ | ✓ | ✓ | ✗ |
|
||||
| default | ✓ | ✓ | ✗ | ✗ |
|
||||
| private | ✓ | ✗ | ✗ | ✗ |
|
||||
|
||||
## 继承
|
||||
|
||||
```java
|
||||
// 父类
|
||||
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 + "正在汪汪叫");
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
Dog dog = new Dog("旺财");
|
||||
dog.eat(); // 旺财正在吃东西
|
||||
dog.bark(); // 旺财正在汪汪叫
|
||||
```
|
||||
|
||||
## 多态
|
||||
|
||||
```java
|
||||
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(); // 绘制矩形
|
||||
```
|
||||
|
||||
## 抽象类与接口
|
||||
|
||||
### 抽象类
|
||||
|
||||
```java
|
||||
public abstract class Vehicle {
|
||||
protected String brand;
|
||||
|
||||
public Vehicle(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
// 抽象方法
|
||||
public abstract void start();
|
||||
|
||||
// 具体方法
|
||||
public void showBrand() {
|
||||
System.out.println("品牌:" + brand);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 接口
|
||||
|
||||
```java
|
||||
public interface Flyable {
|
||||
void fly();
|
||||
|
||||
default void show() {
|
||||
System.out.println("可以飞行");
|
||||
}
|
||||
}
|
||||
|
||||
public class Bird implements Flyable {
|
||||
@Override
|
||||
public void fly() {
|
||||
System.out.println("鸟在飞翔");
|
||||
}
|
||||
}
|
||||
```
|
||||
198
docs/java/thread.md
Normal file
198
docs/java/thread.md
Normal file
@@ -0,0 +1,198 @@
|
||||
# 多线程编程
|
||||
|
||||
## 线程基础
|
||||
|
||||
### 创建线程
|
||||
|
||||
#### 方式一:继承Thread类
|
||||
|
||||
```java
|
||||
public class MyThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("线程运行中:" + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
MyThread thread = new MyThread();
|
||||
thread.start();
|
||||
```
|
||||
|
||||
#### 方式二:实现Runnable接口
|
||||
|
||||
```java
|
||||
public class MyRunnable implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
System.out.println("线程运行中:" + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
Thread thread = new Thread(new MyRunnable());
|
||||
thread.start();
|
||||
|
||||
// 使用Lambda表达式
|
||||
Thread thread2 = new Thread(() -> {
|
||||
System.out.println("Lambda线程运行中");
|
||||
});
|
||||
thread2.start();
|
||||
```
|
||||
|
||||
#### 方式三:实现Callable接口
|
||||
|
||||
```java
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.FutureTask;
|
||||
|
||||
public class MyCallable implements Callable<Integer> {
|
||||
@Override
|
||||
public Integer call() throws Exception {
|
||||
int sum = 0;
|
||||
for (int i = 1; i <= 100; i++) {
|
||||
sum += i;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
||||
// 使用
|
||||
FutureTask<Integer> futureTask = new FutureTask<>(new MyCallable());
|
||||
Thread thread = new Thread(futureTask);
|
||||
thread.start();
|
||||
|
||||
// 获取返回值
|
||||
Integer result = futureTask.get();
|
||||
System.out.println("1到100的和:" + result); // 5050
|
||||
```
|
||||
|
||||
## 线程同步
|
||||
|
||||
### synchronized关键字
|
||||
|
||||
```java
|
||||
public class Counter {
|
||||
private int count = 0;
|
||||
|
||||
// 同步方法
|
||||
public synchronized void increment() {
|
||||
count++;
|
||||
}
|
||||
|
||||
// 同步代码块
|
||||
public void decrement() {
|
||||
synchronized (this) {
|
||||
count--;
|
||||
}
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Lock接口
|
||||
|
||||
```java
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
public class SafeCounter {
|
||||
private int count = 0;
|
||||
private final Lock lock = new ReentrantLock();
|
||||
|
||||
public void increment() {
|
||||
lock.lock();
|
||||
try {
|
||||
count++;
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 线程通信
|
||||
|
||||
### wait/notify
|
||||
|
||||
```java
|
||||
public class ProducerConsumer {
|
||||
private final Object lock = new Object();
|
||||
private boolean hasData = false;
|
||||
|
||||
public void produce() throws InterruptedException {
|
||||
synchronized (lock) {
|
||||
while (hasData) {
|
||||
lock.wait(); // 等待消费
|
||||
}
|
||||
// 生产数据
|
||||
hasData = true;
|
||||
System.out.println("生产数据");
|
||||
lock.notify(); // 通知消费者
|
||||
}
|
||||
}
|
||||
|
||||
public void consume() throws InterruptedException {
|
||||
synchronized (lock) {
|
||||
while (!hasData) {
|
||||
lock.wait(); // 等待生产
|
||||
}
|
||||
// 消费数据
|
||||
hasData = false;
|
||||
System.out.println("消费数据");
|
||||
lock.notify(); // 通知生产者
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 线程池
|
||||
|
||||
```java
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
// 创建固定大小线程池
|
||||
ExecutorService executor = Executors.newFixedThreadPool(5);
|
||||
|
||||
// 提交任务
|
||||
for (int i = 0; i < 10; i++) {
|
||||
final int taskId = i;
|
||||
executor.submit(() -> {
|
||||
System.out.println("任务 " + taskId + " 由 " +
|
||||
Thread.currentThread().getName() + " 执行");
|
||||
});
|
||||
}
|
||||
|
||||
// 关闭线程池
|
||||
executor.shutdown();
|
||||
```
|
||||
|
||||
### ThreadPoolExecutor
|
||||
|
||||
```java
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
ThreadPoolExecutor executor = new ThreadPoolExecutor(
|
||||
2, // 核心线程数
|
||||
5, // 最大线程数
|
||||
60L, // 空闲线程存活时间
|
||||
TimeUnit.SECONDS, // 时间单位
|
||||
new ArrayBlockingQueue<>(10) // 任务队列
|
||||
);
|
||||
|
||||
executor.execute(() -> {
|
||||
System.out.println("执行任务");
|
||||
});
|
||||
|
||||
executor.shutdown();
|
||||
```
|
||||
Reference in New Issue
Block a user