11922911

|
分享:
▲
▼
更新 版本 1.1.0 (2024-3-15) :加入多重继承功能 (详见 oo_multiple_inheritance.sma), 多重继承是仿照类似 python 的 MRO order 修正 oo_get 和 oo_set 不能使用类别名称搜索 ex: oo_get(@this, "Class@var") 多重继承范例:#include <amxmodx> #include <oo>
public oo_init() { oo_class("B1"); { oo_var("B1", "a", 1); oo_ctor("B1", "Ctor", @cell); oo_mthd("B1", "Print"); oo_mthd("B1", "Method1"); }
oo_class("B2"); { oo_var("B2", "b", 1); oo_ctor("B2", "Ctor", @cell); oo_mthd("B2", "Print"); oo_mthd("B2", "Method2"); }
oo_class("D", "B1", "B2"); { oo_var("D", "hp", 1); oo_ctor("D", "Ctor", @cell, @cell, @cell); oo_mthd("D", "Print"); } }
public plugin_init() { new obj = oo_new("D", 100, 689, 777); oo_call(obj, "Print"); }
public B1@Ctor(a) { oo_set(oo_this(), "a", a); }
public B2@Ctor(b) { oo_set(oo_this(), "b", b); }
public D@Ctor(hp, a, b) { oo_super_ctor("B1", a); oo_super_ctor("B2", b); oo_set(@this, "hp", hp); }
public D@Print() { oo_call(@this, "Method1"); oo_call(@this, "Method2");
oo_call(@this, "B1@Print"); oo_call(@this, "B2@Print"); server_print("D@Print(hp=%d, a=%d, b=%d)", oo_get(@this, "hp"), oo_get(@this, "a"), oo_get(@this, "b")); }
public B1@Method1() { server_print("B1@Method1()"); } public B2@Method2() { server_print("B2@Method2()"); }
public B1@Print() { server_print("B1@Print()"); } public B2@Print() { server_print("B2@Print()"); }
输出结果 B1@Method1() B2@Method2() B1@Print() B2@Print() D@Print(hp=100, a=689, b=777)
[ 此文章被11922911在2024-03-17 10:29重新编辑 ]
|
|
x1
[5 楼]
From:香港没有资料 | Posted:2024-03-15 20:37 |
|
|
11922911

|
分享:
▲
今天在 GitHub 最新的 commit 加入了一个实验性的功能, 但还在测试中 这个功能是 HOOK 挂钩系统, 有了这功能你就可以挂钩所有插件的 OO 方法/建构和解构子 如果你想测试, 你可以到我那个 repo 的 Actions 页面下载最新由 GitHub workflows 自动建置生成好的档案 和下载最新的 oo.inc档案会在 Action 中的 Artifacts 的区块 (你需要登入 GitHub 才能查看可以下载的档案) HOOK 挂钩系统 详细使用方法: (在这个例子中我们将挂钩 oo_animal.sma 中的东西) 复制程式
#include <amxmodx>
#include <oo>
public plugin_init()
{
// 挂钩 Animal@Ctor() 的建构子
oo_hook_ctor("Animal", "Ctor", "OnAnimalCtor");
// 挂钩 Dog@MakeSound() 方法 (post事件)
oo_hook_mthd("Dog", "MakeSound", "OnDogMakeSound_Post", 1);
// 挂钩 Snake@GetLegs() 方法
oo_hook_mthd("Snake", "GetLegs", "OnSnakeGetLegs_1");
oo_hook_mthd("Snake", "GetLegs", "OnSnakeGetLegs_2");
// 挂钩 Snake@Test() 方法
oo_hook_mthd("Snake", "Test", "OnSnakeTest");
// 挂钩 Animal@Dtor() 的解构子
oo_hook_dtor("Animal", "OnAnimalDtor");
}
public OnAnimalCtor(const name[], age)
{
server_print("OnAnimalCtor(name=%s, age=%d)", name, age);
}
public OnDogMakeSound_Post(msg[], len)
{
server_print("OnDogMakeSound_Post(msg=%s, len=%d)", msg, len);
}
public OnSnakeGetLegs_1()
{
server_print("OnSnakeGetLegs_1()");
oo_hook_set_return(369); // 改变这方法原本回传的值
return OO_CONTINUE;
}
public OnSnakeGetLegs_2()
{
server_print("OnSnakeGetLegs_2() => oo_hook_get_return()=%d",
oo_hook_get_return()); // 获取之前改变了的回传值
return OO_SUPERCEDE; // 阻止原本这方法的执行
}
public OnSnakeTest(a, &b, const c[], d[], e[5])
{
server_print("OnSnakeTest(%d, %d, %s, %s, {%d,%d,%d,%d,%d})", a, b, c ,d, e[0], e[1], e[2], e[3], e[4]);
// 改变第 1 个参数 a 的值做 11
oo_hook_set_param(1, OO_CELL, 11);
// 改变第 2 个参数 b 的值做 22 (因为类型是 OO_BYREF 可以直接改)
b = 22;
// 改变第 3 个参数 c 的值做 "33"
oo_hook_set_param(3, OO_STRING, "33");
// 改变第 4 个参数 d 的值做 "44" (因为类型是 OO_STRING_REF 可以直接改)
copy(d, 31, "44");
// 改变第 5 个参数 e 的值做 {11, 22, 33, 44, 55}
new arr[5] = {11, 22, 33, 44, 55};
e = arr; // 因为类型是 OO_ARRAY 可以直接改
}
public OnAnimalDtor()
{
server_print("OnAnimalDtor()");
}
[ 此文章被11922911在2024-06-28 17:46重新编辑 ]
|
|
x0
[6 楼]
From:香港没有资料 | Posted:2024-06-28 16:36 |
|
|
|