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 |
|
|
|