我正在製作一個控制檯RPG,並且對泛型非常新穎。我的問題是,我想要一個英雄(存儲在英雄列表通用)攻擊怪物(存儲在敵人列表通用)。我擁有的問題是讓英雄從我的怪物中選擇一個目標,然後修改怪物的hpcurrent。對於一個怪物和英雄來說這很容易,但是當我擴展遊戲時,我打算擁有幾種類型的怪物和英雄;因此我想爲我的英雄使用一段代碼,它可以從通用中挑選一件物品並修改它的統計數據。有兩種泛型的原因是因爲它讓我更容易看到所有英雄或怪物是否都死了。C#如何修改列表<class>
我會後對怪物的代碼(這等同於英雄的,這些數字都只是不同):
class Orc : Character
{
public Orc()
{
this.hpcurrent = 12;
this.hpmax = 12;
this.mpcurrent = 0;
this.mpmax = 0;
this.strength = 6;
this.defense = 4;
this.magicstrength = 0;
this.magicdefense = 2;
this.agility = 4;
this.level = 3;
this.isaliveBool = true;
this.name = "Orc";
this.weakness1 = "fire";
this.weakness2 = "thunder";
this.battlemove = null;
this.typeofcharacter = "monster";
}
的怪物列表中的代碼是:
public List<Character> enemies()
{
List<Character> enemies = new List<Character>();
enemies.Clear();
enemies.Add(new Orc());
return enemies;
}
聲明'enemies.Clear();'是無用的,因爲你剛剛在上一行實例化了列表......你正在清空一個空集合。 –
表示,我不明白正在嘗試做什麼。你在調用「修改」列表是什麼?也許你想繼承'CollectionBase':'公共類CharacterList:CollectionBase {...有些東西不在列表 ...}'? –