代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

Python类型 Python

类型注释示例

from typing import List, Tuple, Dict, Optional

def greet(name: str) -> str:
    return f"Hello, {name}!"

def process_data(data: List[int]) -> float:
    return sum(data) / len(data)

User = Dict[str, int]
Address = Tuple[str, int, str]

def get_user(user_id: int) -> Optional[User]:
    if user_id == 1:
        return {"name": "Alice", "age": 30}
    return None
Java模式匹配 Java

instanceof增强

public class PatternMatching {
    public static double calculate(Object shape) {
        if (shape instanceof Rectangle rect) {
            return rect.width() * rect.height();
        } else if (shape instanceof Circle circle){
            return Math.PI * Math.pow(circle.radius(), 2);
        }
        return 0;
    }
    
    public static void main(String[] args) {
        Object shape = new Rectangle(5, 10);
        double area = calculate(shape);
        System.out.println("Area: " + area);
    }
}

class Rectangle {
    private double width;
    private double height;
    
    public Rectangle(double width, double height) {
        this.width = width;
        this.height = height;
    }
    
    public double width() { return width; }
    public double height() { return height; }
}

class Circle {
    private double radius;
    
    public Circle(double radius) {
        this.radius = radius;
    }
    
    public double radius() { return radius; }
}
Java反射 Java

动态调用方法

public class ReflectionDemo {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = Class.forName("java.util.ArrayList");
        Object list = clazz.getDeclaredConstructor().newInstance();
        Method addMethod = clazz.getMethod("add", Object.class);
        addMethod.invoke(list, "Hello Reflection!");
        System.out.println(list);
    }
}
Java文件读写 Java

NIO文件操作

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileIODemo {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("file.txt");
        
        // Write to file
        String content = "Hello, NIO File API!";
        Files.write(path, content.getBytes(StandardCharsets.UTF8));
        
        // Read from file
        byte[] readBytes = Files.readAllBytes(path);
        System.out.println(new String(readBytes, StandardCharsets.UTF8));
    }
}
C++线程 C++

std::thread使用

#include <iostream>
#include <thread>
#include <vector>
using namespace std;

void thread_function(int id) {
    cout << "Thread " << id << " is running\n";
}

int main() {
    vector<thread> threads;
    
    for (int i = 0; i < 5; ++i) {
        threads.push_back(thread(thread_function, i));
    }
    
    for (auto& t : threads) {
        t.join();
    }
    
    cout << "All threads normally terminated\n";
    return 0;
}
Java枚举 Java

枚举类型示例

public enum DayOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
    
    public String getDisplayName() {
        switch (this) {
            case MONDAY: return "Monday";
            case SATURDAY: return "Weekend";
            default: return "Unknown";
        }
    }
}

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类