如何使用构造函数重载来创建2个独立的"仓库"?

时间:2016-11-26 21:33:15

标签: c#

对于我本周在课堂上的任务,我们要重载"仓库构造函数"。第一步是使用构造函数设置具有0个无线电,计算机和电视的仓库类。还应该有按钮添加到每个项目的库存中。所有人的起始库存应为0.我不确定我是否正确执行了此操作。

然后我们必须重载该构造函数并使用预先指定的数字创建一个仓库。我做了这个,但是当我打电话给我的第一个仓库时,我的无线电数量为0 ......

我对这个话题非常困惑,并且正在寻找是否有人可以帮助更好地解释我的问题是什么?非常感谢。

这是我的仓库类:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static int R, C, T;

        public static int R1
        {
            get
            {
                return R;
            }

            set
            {
                R = value;
            }
        }

        public static int C1
        {
            get
            {
                return C;
            }

            set
            {
                C = value;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Text = warehouse.Radios.ToString();
        }

        public static int T1
        {
            get
            {
                return T;
            }

            set
            {
                T = value;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}

接下来是我展示仓库的表格。

{{1}}

我打算在一个单独的TextBox中显示仓库中的每个项目,然后在下面按下按钮,在按下每个按钮时将库存加1。

截至目前,当我按下显示仓库时,只会弹出一个零。

3 个答案:

答案 0 :(得分:0)

在这部分......

    public warehouse (int r, int t, int c)
    {
        r = Radios;
        t = Televisions;
        c = Computers;
    }

...您将参数放在赋值的左侧,将属性放在右侧。这意味着您使用属性值(使用默认值0初始化)来覆盖参数。你没有设置任何财产。你必须反过来做:从参数设置属性。

答案 1 :(得分:0)

你需要做的第一件事就是不创建对象,不能覆盖类成员的值。在那里,你应该在 Form1 类里面为你的仓库类创建对象然后每一个事情会很好。

 public warehouse (int r, int t, int c)
{
    r = Radios;
    t = Televisions;
    c = Computers;
}

这里你以错误的方式分配值我认为。应该为你的属性而不是参数赋值。

答案 2 :(得分:0)

仓库类:

class Warehouse
{
    private int radios, televisions, computers;

    public int Radios
    {
        get { return radios; }
        set { radios = value; }
    }

    public int Televisions
    {
        get { return televisions; }
        set { televisions = value; }
    }

    public int Computers
    {
        get { return computers; }
        set { computers = value; }
    }

    public Warehouse(int radios, int televisions, int computers)
    {
        this.radios = radios;
        this.televisions = televisions;
        this.computers = computers;
    }

    public Warehouse() : this(5, 5, 5) { }
}

这里,字段和属性是非静态的(实例成员)。静态成员是为全局类维护的,这意味着只有一个副本,因此您不能拥有多个仓库。实例成员作为每个实例(每个对象)的单独副本进行维护,因此您可以维护多个仓库。

如果需要,可以将属性定义为自动实现的属性。

示例表单:

public partial class Form1 : Form
{
    private Warehouse warehouse1;
    private Warehouse warehouse2;

    public Form2()
    {
        InitializeComponent();

        warehouse1 = new Warehouse();
        warehouse2 = new Warehouse(6, 7, 8);
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
        textBox1.Text = warehouse1.Radios.ToString();
        textBox2.Text = warehouse1.Televisions.ToString();
        textBox3.Text = warehouse1.Computers.ToString();
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
        textBox1.Text = warehouse2.Radios.ToString();
        textBox2.Text = warehouse2.Televisions.ToString();
        textBox3.Text = warehouse2.Computers.ToString();
    }
}

在这里,当你点击button1时,仓库1的详细信息将显示在文本框中,仓库2将显示为button2 ...