我的学校任务有问题。我们必须创建一个Inventory类并用Objects填充它。库存应具有最大容量。第二个任务是编写一个方法来总结我的对象的总权重(浮点数)。 我不知道如何在库存中获取对象..请帮助!!这是我的代码: 类:
public abstract class Item1 { //abstract BASE
string label;
float weight;
protected Item1 (string l, float w) {
label = l;
weight = w;
}
public string Label() { return label; }
public float Weight() { return weight; }
}
public abstract class Equipment : Item1 { //abstact
float tear;
protected Equipment (string l, float w, float t) : base (l,w) {
tear = t;
}
public float Tear() { return tear; }
}
public abstract class Goods : Item1 { //abstact
float uselevel;
protected Goods (string l, float w, float ul) : base (l,w) {
uselevel = ul;
}
public float Uselevel() { return uselevel; }
}
class Sword : Equipment { //concrete
public float damage;
public Sword (string l, float w, float t, float d) : base (l,w,t) {
damage = d;
}
public override string ToString ()
{
return "Item: " + Label () +" +" + damage + " damage" ;
}
}
class Shield : Equipment { //concrete
public float block;
public Shield (string l, float w, float t, float b) : base (l,w,t) {
block = b;
}
public override string ToString ()
{
return "Item: " + Label () +" +" + block + " defence" ;
}
}
class HP : Goods { //concrete
public float heal;
public HP (string l, float w, float ul, float h) : base (l,w,ul) {
heal = h;
}
public override string ToString ()
{
return "Item: " + Label () +" +" + heal + " health" ;
}
}
class MP : Goods { //concrete
public float mana;
public MP (string l, float w, float ul, float m) : base (l,w,ul) {
mana = m;
}
public override string ToString ()
{
return "Item: " + Label () +" +" + mana + " mana" ;
}
}
Program:
public class Build : MonoBehaviour {
void Start() {
}
void Update () {
if(Input.GetKey(KeyCode.F)) {
//Inventory.items.Add( new Sword("Lucifers Blade",12,15,2130) );
//Inventory.items.Add( new HP("Healpotion",1,2,850) );
//Inventory.items.Add( new MP("Manapotion",1,2,1300) );
//Sword sword = new Sword("Lucifers Blade",12,15,1337);
//print (sword.Label() + " weight: " + sword.Weight() + " KG - Level: "+ sword.Tear() + " DMG: " + sword.damage );
}
if(Input.GetKey(KeyCode.D)) {
MP mana = new MP("Manapotion",12,15,1337);
print ("You used: " + mana.Label() + "(Weight: " + mana.Weight() + " kg) Level: " + mana.Uselevel() + " + " + mana.mana + " Mana.");
}
}
}
Inventory:
using System.Collections.Generic;
public class Inventory {
string name;
public static List<Item1> items;
public Inventory(string n) {
name = n;
items = new List<Item1>();
}
public void Add(Item1 item) {
items.Add (item);
items.Add( new Sword("Lucifers Blade",12,15,2130) );
}
public void Remove(Item1 item) {
items.Remove (item);
}
public override string ToString (){
string s = name + " contains ";
foreach (Item1 item in items)
s += item + ", ";
return s;
}
}
答案 0 :(得分:1)
您正在使用Inventory作为静态类,您需要创建它的实例。
Inventory myInventory = new Inventory("Inventory");
然后添加项目
myInventory.items.Add( new MP("Manapotion",1,2,1300) );
最后,您可以使用Linq计算出重量
float weight = myInventory.items.Sum(item => item.weight);
此外,列表是静态的似乎很奇怪。这是为了吗?
答案 1 :(得分:0)
我不知道在家庭作业问题上的政策是什么,但希望这会让你回到正轨:
您必须创建一个Inventory对象,如:
库存盘点=新库存(“背包”);
然后你需要添加项目:
inventory.add(新剑(“Lucifers Blade”,12,15,2130));
(并将Lucifers Blade系列从Inventory类中删除,否则每次添加其他内容时都会添加它。)