【问题标题】:Adding Objects to List and summing up the total weight将对象添加到列表并总结总重量
【发布时间】:2013-11-16 19:04:34
【问题描述】:

我对学校的任务有疑问。我们必须创建一个库存类并用对象填充它。库存应具有最大容量。第二个任务是编写一个方法来总结我的对象的总重量(浮动)。 我不知道如何在库存中获取对象..请帮助!这是我的代码: 类:

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;
    }

}

【问题讨论】:

    标签: c# list class oop inventory


    【解决方案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);
    

    此外,列表是静态的似乎很奇怪。这是故意的吗?

    【讨论】:

    • @user3000032 没问题。很高兴我能帮忙:)
    • 但让脚本运行的唯一方法是声明 myinventory 静态.. 我认为这是一个统一问题
    【解决方案2】:

    我不知道 SO 对家庭作业问题的政策是什么,但希望这能让你重回正轨:

    你必须创建一个 Inventory 对象,如下所示:

    库存inventory = new Inventory("Backpack");

    然后你需要给它添加项目:

    inventory.add(new Sword("Lucifers Blade",12,15,2130));

    (并将 Lucifers Blade 系列从您的 Inventory 类中取出,否则它会在您每次添加其他任何内容时添加它)。

    【讨论】:

    • 家庭作业问题是可以接受的,只要作者已经表现出先前的研究和尝试的迹象,而不是只是想用勺子喂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2021-07-16
    相关资源
    最近更新 更多