mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-13 00:10:27 +00:00
feat: 装饰器与装饰器路由注册
This commit is contained in:
31
packages/napcat-shell/utils/test.ts
Normal file
31
packages/napcat-shell/utils/test.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// 日志装饰器
|
||||
function log2(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
const originalMethod = descriptor.value;
|
||||
|
||||
descriptor.value = function (...args: any[]) {
|
||||
console.log(`Calling ${propertyKey} with args: ${JSON.stringify(args)}`);
|
||||
const result = originalMethod.apply(this, args);
|
||||
console.log(`${propertyKey} returned: ${result}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
// 示例类
|
||||
class MathOperations2 {
|
||||
@log2
|
||||
add(a: number, b: number): number {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@log2
|
||||
multiply(a: number, b: number): number {
|
||||
return a * b;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建实例并调用方法
|
||||
const math2 = new MathOperations2();
|
||||
math2.add(1, 2); // 调用加法
|
||||
math2.multiply(3, 4); // 调用乘法
|
||||
31
packages/napcat-shell/utils/test2.ts
Normal file
31
packages/napcat-shell/utils/test2.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// 日志装饰器
|
||||
function log(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
||||
const originalMethod = descriptor.value;
|
||||
|
||||
descriptor.value = function (...args: any[]) {
|
||||
console.log(`Calling ${propertyKey} with args: ${JSON.stringify(args)}`);
|
||||
const result = originalMethod.apply(this, args);
|
||||
console.log(`${propertyKey} returned: ${result}`);
|
||||
return result;
|
||||
};
|
||||
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
// 示例类
|
||||
class MathOperations {
|
||||
@log
|
||||
add(a: number, b: number): number {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
@log
|
||||
multiply(a: number, b: number): number {
|
||||
return a * b;
|
||||
}
|
||||
}
|
||||
|
||||
// 创建实例并调用方法
|
||||
const math = new MathOperations();
|
||||
math.add(1, 2); // 调用加法
|
||||
math.multiply(3, 4); // 调用乘法
|
||||
Reference in New Issue
Block a user