如何定义获取和设置数组数据成员? [英] How do define get and set for an array data member?

查看:167
本文介绍了如何定义获取和设置数组数据成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个类客户具有下列数据成员和属性:

I am making a class Customer that has the following data members and properties:

private string customerName;
private double[] totalPurchasesLastThreeDays; //array of 3 elements that will hold the totals of how much the customer purchased for the past three days i.e. element[0] = 100, element[1] = 50, element[2] = 250

public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}

public double[] TotalPurchasesLastThreeDays
{
?
}



如何定义get和为数组数据成员设置?

How do I define the get and set for the array data member?

推荐答案

您可以使用自动属性:

public class Customer
{
    public string CustomerName { get; set; }

    public double[] TotalPurchasesLastThreeDays { get; set; }
}



或者,如果你想要的:

Or if you want:

public class Customer
    {
        private double[] totalPurchasesLastThreeDays; 

        public string CustomerName { get; set; }

        public double[] TotalPurchasesLastThreeDays
        {
            get
            {
                return totalPurchasesLastThreeDays;
            }
            set
            {
                totalPurchasesLastThreeDays = value;
            }
        }
    }



然后在构造函数中,您可以设置一些默认值:

And then in the constructor, you can set some default values:

public Customer()
{
    totalPurchasesLastThreeDays = new double[] { 100, 50, 250 };
}

这篇关于如何定义获取和设置数组数据成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆