我正在为一个小型的自学项目建立一个小“商店”系统。如果我定义表单接受的每个类参数,我可以使它工作,但如果我想传递具有不同属性的不同类,该怎么办?
这是我的项目类,每个都有不同的属性。
public class Items
{
public int ID { get; set; }
public string ItemName { get; set; }
public int ItemType { get; set; }
public int GoldValue { get; set; }
}
public class Weapons : Items
{
public int MinDamage { get; set; }
public int MaxDamage { get; set; }
public Weapons(int id, string itemName, int itemType, int goldValue, int minDamage, int maxDamage)
{
ID = id;
ItemName = itemName;
ItemType = itemType;
GoldValue = goldValue;
MinDamage = minDamage;
MaxDamage = maxDamage;
}
}
public class Armors : Items
{
public int Defense { get; set; }
public Armors(int id, string itemName, int itemType, int goldValue, int defense)
{
ID = id;
ItemName = itemName;
ItemType = itemType;
GoldValue = goldValue;
Defense = defense;
}
}
public class Shields : Items
{
public int Defense { get; set; }
public Shields(int id, string itemName, int itemType, int goldValue, int defense)
{
ID = id;
ItemName = itemName;
ItemType = itemType;
GoldValue = goldValue;
Defense = defense;
}
}
public class Potions : Items
{
public int HPHeal { get; set; }
public int MPHeal { get; set; }
public int SPHeal { get; set; }
public Potions(int id, string itemName, int itemType, int goldValue, int hpHeal, int mpHeal, int spHeal)
{
ID = id;
ItemName = itemName;
ItemType = itemType;
GoldValue = goldValue;
HPHeal = hpHeal;
MPHeal = mpHeal;
SPHeal = spHeal;
}
}
这是我的商店表格。
public partial class Shop : Form
{
Weapons item1;
Armors item2;
Shields item3;
int Gold;
public Shop(int gold, Weapons weapon1, Armors armor1, Shields shield1)
{
InitializeComponent();
item1 = weapon1;
item2 = armor1;
Gold = gold;
item1Lbl.Text = item1.ItemName + " " + item1.MinDamage + " - " + item1.MaxDamage + " Damage -- " + item1.GoldValue + " Gold";
item2Lbl.Text = item2.ItemName + " " + item2.Defense + " Defense -- " + item2.GoldValue + " Gold";
item3Lbl.Text = item3.ItemName + " " + item3.Defense + " Defense -- " + item3.GoldValue + " Gold";
}
public Shop(int gold, Potions potion1, Potions potion2, Potions potion3)
{
}
private void Shop_Load(object sender, EventArgs e)
{
}
private void buy1Btn_Click(object sender, EventArgs e)
{
if (Gold >= item1.GoldValue)
{
Equipment.ChangeWeapon(item1.ID, item1.ItemName, item1.MinDamage, item1.MaxDamage);
Equipment.Gold -= item1.GoldValue;
this.Close();
}
else
{
MessageBox.Show("You need " + (item1.GoldValue - Gold) + " additional gold.");
}
}
private void buy2Btn_Click(object sender, EventArgs e)
{
if (Gold >= item2.GoldValue)
{
Equipment.ChangeArmor(item2.ID, item2.ItemName, item2.Defense);
Equipment.Gold -= item2.GoldValue;
this.Close();
}
else
{
MessageBox.Show("You need " + (item2.GoldValue - Gold) + " additional gold.");
}
}
private void buy3Btn_Click(object sender, EventArgs e)
{
if (Gold >= item3.GoldValue)
{
Equipment.ChangeShield(item3.ID, item3.ItemName, item3.Defense);
Equipment.Gold -= item3.GoldValue;
this.Close();
}
else
{
MessageBox.Show("You need " + (item3.GoldValue - Gold) + " additional gold.");
}
}
}
设置它的有效方法是什么,以便我可以将任何项目类型(武器,装甲,药水等)传递给同一个表单而不必为每种可能性明确编写代码?