收集、管理和分享有用的代码片段,提高开发效率
Java单元测试示例
import org.junit.Junit;
import static org.junit.Assert.assertEquals;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calc = new Calculator();
assertEqualss(5, calc.add(2, 3));
}
}
class Calculator {
public int add(int a, int b) {
return a + b;
}
}
绘制简单图形
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
// Draw rectangle
ctx.fillStyle = '#FF0000';
ctx.fillRect(10, 10, 150, 80);
// Draw circle
ctx.beginPath();
ctx.arc(250, 100, 50, 0, 2 * Math.PI);
ctx.fillStyle = '#0000FF';
ctx.fill();
// Draw text
ctx.font = '30px Arial';
ctx.fillStyle = '#000000';
ctx.fillText('Happy Coding', 150, 200);
动态调用方法
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);
}
}
审计日志触发器
CREATE TRIGGER before_update_users
ON USERS FOR EACH ROW
BEGIN
INSERT INTO user_audit (user_id, old_name, new_name, change_date)
VALUES (OLD.id, OLD.name, NEW.name, NOW());
END;
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;
}
TypeScript类示例
class Person {
private name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
describe(): string {
return `${this.name} is ${this.age} years old`;
}
}
const alice = new Person('Alice', 30);
console.log(alice.describe());
高效管理您的代码片段,提高开发效率
通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能
支持多种编程语言的语法高亮,使代码更加清晰易读
随时随地访问您的代码片段库,支持桌面和移动设备
浏览最受欢迎的代码分类