廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 2357 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
11922911
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x31 鮮花 x69
分享: 轉寄此文章 Facebook Plurk Twitter 版主評分版主評分版主評分 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x2
[程式] 實現AMXX物件導向編程(OOP)的模塊 1.10 版本 (2024/3/15 更新)  (模塊 DLL)
原創文章
【插件資訊】

插件來源:原創 https://forums.alliedmods.net...php?t=343808
使用指令:沒有
安裝路徑:gamemod/addons/amxmodx
版本:1.1.0

【插件介紹】

安裝需求:

  • 必須使用 AMXX 1.8.3 或以上




下載地址:
https://github.com/hollacs/o...ases/latest



安裝方法:
下載後放到 addons/amxmodx
裡面有兩個測試範例插件, 請自行編譯並測試能不能正常運作 (正常應該會在控制台顯示正確的輸出訊息)



源碼: (怕的話可以自行編譯)
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", oo_class_exists("Bird") ? "EXISTS" : "DOES NOT EXIST");
     server_print("Class Fish %s", oo_class_exists("Fish") ? "EXISTS" : "DOES NOT EXIST");

     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", animals[0], oo_object_exists(animals[0]) ? "EXISTS" : "DOES NOT EXIST");

     for (new j = 0; j < 5; j++)
     {
           oo_delete(animals[j]); // Delete each animal objects
     }

     server_print("Object #%d %s", animals[0], oo_object_exists(animals[0]) ? "EXISTS" : "DOES NOT EXIST");
}


輸出結果
你好, 我的名字叫 Dog, 現在 7 歲, 我有 4 條腿, 我會說 汪汪汪
你好, 我的名字叫 Cat, 現在 6 歲, 我有 4 條腿, 我會說 喵喵喵
你好, 我的名字叫 Bird, 現在 4 歲, 我有 2 條腿, 我會說 咕咕咕
你好, 我的名字叫 Snake, 現在 3 歲, 我有 0 條腿, 我會說 Sss sss
你好, 我的名字叫 Unknown, 現在 0 歲, 我有 0 條腿, 我會說 我是一個動物
靜態方法測試
Object #1943714116 IS a Snake
Object #1943714116 IS NOT a Dog
Class Dog IS a subclass of Animal
Class Animal IS NOT a subclass of Cat
Class Bird EXISTS
Class Fish DOES NOT EXIST
Object #461123433's classname is Dog
Object #461123433 EXISTS
Dog 已經被人道毀滅
Cat 已經被人道毀滅
Bird 已經被人道毀滅
Snake 已經被人道毀滅
Unknown 已經被人道毀滅
Object #461123433 DOES NOT EXIST


更多使用方法說明請看 oo.inc


已知問題/限制:

  • 建構子(constructor) 跟 方法(method) 之中不可使用 預設值(default parameter)

  • 類別的變數不支援二維或多維陣列 (2d array)




特別感謝 CHATGPT 先生為我打了文章的大部分內容跟範例程式碼


[ 此文章被11922911在2024-03-15 23:48重新編輯 ]

此文章被評分,最近評分記錄
財富:500 (by amore12) | 理由: 辛苦了!!



YouTube: @holla16
獻花 x4 回到頂端 [樓 主] From:香港沒有資料 | Posted:2023-07-13 18:16 |
cyxnzb 會員卡
個人文章 個人相簿 個人日記 個人地圖
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x0 鮮花 x6
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

這個直觀來說能實現什麼功能呢請問


獻花 x0 回到頂端 [1 樓] From:未知地址 | Posted:2023-07-14 17:43 |
11922911
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x31 鮮花 x69
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

下面是引用 cyxnzb 於 2023-07-14 17:43 發表的 : 到引言文
這個直觀來說能實現什麼功能呢請問

這其實不是實現甚麼功能的, 這是讓編寫插件的人能使用OOP的方式來寫插件, 讓插件開發過程更方便

使用OOP來寫東西的好處是能減少重複的程式碼, 程式可被高度重複使用, 減少撰寫程式錯誤的風險, 程式更加易於維護 易擴展

詳情可以到各大搜尋引擎找找看 "OOP" 或者 "物件導向程式設計", 希望能解答到你的疑問 表情

此文章被評分,最近評分記錄
財富:300 (by amore12)


YouTube: @holla16
獻花 x1 回到頂端 [2 樓] From:香港沒有資料 | Posted:2023-07-14 18:18 |
Nailaz 手機
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎 創作大師獎
小有名氣
級別: 小有名氣 該用戶目前不上站
推文 x77 鮮花 x253
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

酷東西,對程式好有熱忱啊!


web front-end and software engineer.
獻花 x2 回到頂端 [3 樓] From:臺灣中華電信股份有限公司 | Posted:2023-07-17 10:21 |
11922911
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x31 鮮花 x69
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

更新 2023-7-28:
重寫內核,使用 AMTL 取代 STL,修正 linux 下崩潰的問題
帖子裡加入了新的下載連結


[ 此文章被11922911在2024-03-15 20:41重新編輯 ]

此文章被評分,最近評分記錄
財富:300 (by amore12) | 理由:


YouTube: @holla16
獻花 x3 回到頂端 [4 樓] From:香港沒有資料 | Posted:2023-07-28 01:25 |
11922911
個人頭像
個人文章 個人相簿 個人日記 個人地圖
特殊貢獻獎 社區建設獎
初露鋒芒
級別: 初露鋒芒 該用戶目前不上站
推文 x31 鮮花 x69
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

更新 版本 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重新編輯 ]


YouTube: @holla16
獻花 x1 回到頂端 [5 樓] From:香港沒有資料 | Posted:2024-03-15 20:37 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.019632 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言