11922911

|
分享:
 
▼
x2
|
[程式] 實現AMXX物件導向編程(OOP)的模塊 (2023/9/3 update)
(模塊 DLL)
【插件資訊】插件來源:原創 https://forums.alliedmods.net...php?t=343808使用指令:沒有 安裝路徑:gamemod/addons/amxmodx 【插件介紹】安裝需求:
- 必須使用 AMXX 1.8.3 或以上
Linux下也不再需要 AMXX 1.10
下載地址:https://github.com/hollacs/oo...es/tag/1.0.0 安裝方法:下載對應作業系統的oo_amxx壓縮檔,解壓縮到 amxmodx/modules 然後也要下載 scripting.zip 解壓縮到 amxmodx/scripting 源碼: ( 怕的話可以自行編譯) https://github.com/hollacs...ree/no-std
在許多程式語言中,物件導向編程已經成為一種常見的編程範式。然而,在AMX Mod X (AMXX) 這樣的遊戲伺服器模塊中,物件導向編程並不是很容易實現。為了解決這個問題,我寫了一個可以讓AMXX實現OOP的模塊,使得開發人員可以更方便地使用物件導向編程。 這個模塊的核心是一個名為 OO 的庫,它提供了一個類別系統,允許開發人員創建自己的類別、物件和方法。OO 庫還提供了一些簡單的工具,例如存取類別變數和方法的函數。開發人員只需遵循一些簡單的規則來聲明和使用類別,就可以在AMXX中實現物件導向編程。 為了說明這個模塊的使用方法,讓我們來看一個簡單的例子。 #include <amxmodx> #include <oo>
#define main plugin_init
public oo_init() { // 定義 Animal 類 oo_class("Animal") { new cl[] = "Animal";
oo_var(cl, "age", 1); // 為 Animal 定義變數 age 表示為這動物的年齡 oo_var(cl, "name", 32); // 為 Animal 定義變數 name 表示為這動物的名字
// 定義 Animal 的建構子得到名字和年齡的資料 oo_ctor(cl, "Ctor", @str{name}, @int{age});
// 定義 Animal 的解構子 oo_dtor(cl, "Dtor");
// 定義 MakeSound 方法 用以回傳這動物的聲音 oo_mthd(cl, "MakeSound", @stref{msg}, @int{len});
// 定義 GetLegs 方法 用以回傳這動物有多少條腿 oo_mthd(cl, "GetLegs");
// 定義 Introduce 方法 輸出這動物的自我介紹 oo_mthd(cl, "Introduce");
oo_smthd(cl, "Test"); // 靜態方法測試 }
// 定義 Dog 類繼承自 Animal 類 oo_class("Dog", "Animal") { new cl[] = "Dog";
// 定義建構子, 這會呼叫基類的建構子並輸入 Dog 作為名字 oo_ctor(cl, "Ctor", @int{age});
// 取代原本的 MakeSound 方法 回傳狗的叫聲 oo_mthd(cl, "MakeSound", @stref{msg}, @int{len});
// 取代原本的 GetLegs 方法 回傳狗有多少條腿 oo_mthd(cl, "GetLegs"); }
// 定義 Cat 類繼承自 Animal 類 oo_class("Cat", "Animal") { new cl[] = "Cat";
// 定義建構子, 這會呼叫基類的建構子並輸入 Cat 作為名字 oo_ctor(cl, "Ctor", @int{age});
// 取代原本的 MakeSound 方法 回傳貓的叫聲 oo_mthd(cl, "MakeSound", @stref{msg}, @int{len});
// 取代原本的 GetLegs 方法 回傳貓有多少條腿 oo_mthd(cl, "GetLegs"); }
// 同上 oo_class("Bird", "Animal") { new cl[] = "Bird";
oo_ctor(cl, "Ctor", @int{age});
oo_mthd(cl, "MakeSound", @stref{msg}, @int{len});
oo_mthd(cl, "GetLegs"); }
// 同上 oo_class("Snake", "Animal") { new cl[] = "Snake";
oo_ctor(cl, "Ctor", @int{age});
oo_mthd(cl, "MakeSound", @stref{msg}, @int{len});
oo_mthd(cl, "GetLegs"); } }
// Animal 的建構子 public Animal@Ctor(const name[], age) { new this = oo_this(); // 獲取 this 物件的地址 oo_set_str(this, "name", name); // 為動物的 name 變數賦以字串的值 oo_set(this, "age", age); // 為動物的 age 變數賦以整數的值 }
// Animal 的解構子 public Animal@Dtor() { new name[32]; oo_get_str(oo_this(), "name", name, 32); // 獲取這動物的 name 變數的字串值 server_print("%s 已經被人道毀滅.", name); }
public Animal@MakeSound(msg[], len) { // format the message to the msg[] formatex(msg, len, "我是一個動物"); }
public Animal@GetLegs() { return 0; }
public Animal@Introduce() { new this = oo_this();
new name[32]; oo_get_str(this, "name", name, 32);
new age = oo_get(this, "age"); // 獲取這動物的 age 變數的整數值
new legs = oo_call(this, "GetLegs"); // 呼叫這動物的 GetLegs 方法, 獲取動物的腿數量
// 呼叫這動物的 MakeSound 方法 and retrieve the result to the msg[] new msg[64]; oo_call(this, "MakeSound", msg, charsmax(msg));
server_print("你好, 我的名字是 %s, 現在 %d 歲, 有 %d 條腿, 我會說 %s", name, age, legs, msg); }
public Animal@Test() { server_print("靜態方法測試"); }
public Dog@Ctor(age) { // 呼叫父類 Animal 的建構子 oo_super_ctor("Animal", "Dog", age); }
public Dog@MakeSound(msg[], len) { formatex(msg, len, "汪汪汪"); }
public Dog@GetLegs() { return 4; }
public Cat@Ctor(age) { oo_super_ctor("Animal", "Cat", age); }
public Cat@MakeSound(msg[], len) { formatex(msg, len, "喵喵喵"); }
public Cat@GetLegs() { return 4; }
public Bird@Ctor(age) { oo_super_ctor("Animal", "Bird", age); }
public Bird@MakeSound(msg[], len) { formatex(msg, len, "咕咕咕"); }
public Bird@GetLegs() { return 2; }
public Snake@Ctor(age) { oo_super_ctor("Animal", "Snake", age); }
public Snake@MakeSound(msg[], len) { formatex(msg, len, "Sss sss"); }
public Snake@GetLegs() { return 0; }
public main() { register_plugin("[OO] Animal", "0.1", "holla");
// 建立物件 new Animal:animals[5]; animals[0] = oo_new("Dog", 7); animals[1] = oo_new("Cat", 6); animals[2] = oo_new("Bird", 4); animals[3] = oo_new("Snake", 3); animals[4] = oo_new("Animal", "Unknown", 0);
for (new j = 0; j < 5; j++) { oo_call(animals[j], "Introduce"); // 為每一個物動呼叫自我介紹的方法 }
// Tests oo_call(0, "Animal@Test"); // Test calling the static method
server_print("Object #%d %s a Snake", animals[3], oo_isa(animals[3], "Snake") ? "IS" : "IS NOT"); server_print("Object #%d %s a Dog", animals[3], oo_isa(animals[3], "Dog") ? "IS" : "IS NOT");
server_print("Class Dog %s a subclass of Animal", oo_subclass_of("Dog", "Animal") ? "IS" : "IS NOT"); server_print("Class Animal %s a subclass of Cat", oo_subclass_of("Animal", "Cat") ? "IS" : "IS NOT");
server_print("Class Bird %s exists", oo_class_exists("Bird") ? "IS" : "IS NOT"); server_print("Class Fish %s exists", oo_class_exists("Fish") ? "IS" : "IS NOT");
new class[32]; oo_get_classname(animals[0], class, charsmax(class)); server_print("Object #%d's classname is %s", animals[0], class);
server_print("Object #%d %s exists", animals[0], oo_object_exists(animals[0]) ? "IS" : "IS NOT");
for (new j = 0; j < 5; j++) { oo_delete(animals[j]); // Delete each animal objects }
server_print("Object #%d %s exists", animals[0], oo_object_exists(animals[0]) ? "IS" : "IS NOT"); }
輸出內容 你好, 我的名字叫 Dog, 現在 7 歲, 我有 4 條腿, 我會說 汪汪汪 你好, 我的名字叫 Cat, 現在 6 歲, 我有 4 條腿, 我會說 喵喵喵 你好, 我的名字叫 Bird, 現在 4 歲, 我有 2 條腿, 我會說 咕咕咕 你好, 我的名字叫 Snake, 現在 3 歲, 我有 0 條腿, 我會說 Sss sss 你好, 我的名字叫 Unknown, 現在 0 歲, 我有 0 條腿, 我會說 我是一個動物 靜態方法測試 Object #2102251586 IS a Snake Object #2102251586 IS NOT a Dog Class Dog IS a subclass of Animal Class Animal IS NOT a subclass of Cat Class Bird IS exists Class Fish IS NOT exists Object #16934577's classname is Dog Object #16934577 IS exists Dog 已經被人道毀滅 Cat 已經被人道毀滅 Bird 已經被人道毀滅 Snake 已經被人道毀滅 Unknown 已經被人道毀滅 Object #16934577 IS NOT exists
更多使用方法說明請看 oo.inc 已知問題/限制:
- 建構子(constructor) 跟 方法(method) 之中不可使用 預設值(default parameter)
- 類別的變數不支援二維或多維陣列 (2d array)
特別感謝 CHATGPT 先生為我打了文章的大部分內容跟範例程式碼
[ 此文章被11922911在2023-09-03 16:42重新編輯 ]
此文章被評分,最近評分記錄財富:500 (by amore12) | 理由: 辛苦了!! | |
|
|
|
|
x4
[樓 主]
From:香港沒有資料 | Posted:2023-07-13 18:16 |
|
|
|