1

总初学者问题。我正在尝试为基于 int Quantity 和小数价格计算对象总价的类创建一个方法。这两个都是私有的并且具有实例变量属性分配。当我在方法中使用两个单独的参数时,我无法弄清楚如何访问它们并计算它们。有问题的方法是 GetInvoiceAmount。任何建议将不胜感激

//create invoice class
//intialize instance 

public class Invoice
   {
   public decimal total;  //instance variable to store invoice total

   //public string InitialPartNumber;
   //public string InitialDescription;
   //public int InitialQuantity;
   //public decimal InitialPrice;
   //public decimal InvoiceAmount;
   // auto-imlemented property for class Invoice
   public string PartNumber { get; set; }

   public string Description { get; set; }

   private int quantity; // quantity of items purchased

   private decimal price; // price per item 

   public decimal invoiceAmount;

   public Invoice(string partNumber, string description, int quantity, decimal price)
   {
      PartNumber = partNumber;
      Description = description;
      Quantity = quantity;
      Price = price;


   }//end constructor

   // begin GetInvoiceAmount Method

   public void GetInvoiceAmount()
   {
      invoiceAmount = Price * Quantity;

   }


   //Begin Instance Variable Property Assignment
   public int Quantity
   {
      get
      {
         return quantity;
      } //end get
      set
      {
         if (value >=0 )
         quantity = value;
      } //end set
   }//end property Quantity

   public decimal Price
   {
      get
      {
         return price;
      } //end get
      set
      {
         if ( value >=0 )

         price = value;
      } //end set
   }//end property Price
}//end Invoice class
4

3 回答 3

2

你在寻找这样的东西吗?

public Decimal GetInvoiceAmount()
{
    return this.Price * this.Quantity;    
}

您当前GetInvoiceAmount的实现设置了公共字段invoiceAmount,因此要使用当前方法,您需要执行以下操作:

yourInstance.GetInvoiceAmount();
Decimal amount = yourInstance.invoiceAmount;

这似乎违反直觉,因为该方法说它正在“获取”某些东西。

为清楚起见,我建议这是一个在每次调用时生成的属性(即没有支持字段的属性),如下所示:

public Decimal InvoiceAmount
{
    get { return this.Price * this.Quantity; }
}

然后,您可以删除不再需要的GetInvoiceAmount方法和字段。invoiceAmount

于 2009-09-28T17:56:25.950 回答
1

下面的呢?我将属性设置器设为私有,以避免在对象构造后修改值,但以后可能会更改的数量除外(使用只读属性会更好,但它们首先出现在 C# 4.0 中并使用只读支持字段添加代码有相当多的噪音)。您可以通过使用无符号整数来避免检查非负数。价格已在构造函数中检查。最后,总数由计算属性返回。

public class Invoice
{
   // Setters are private to avoid modifying the values.
   public String PartNumber { get; private set; }
   public String Description { get; private set; }
   public Decimal Price { get; private set; }

   // Quantity has public setter and is an unsigned integer.
   public UInt32 Quantity { get; set; }

   // Computed property for the total.
   public Decimal Total
   {
      get { return this.Quantity * this.Price; }
   }

   public Invoice(
       String partNumber, String description, UInt32 quantity, Decimal price)
   {
      // Check for non-negative price.
      if (price < 0.00M)
      {
          throw new ArgumentOutOfRangeException();
      }

      // Maybe check part number and description, too.

      this.PartNumber = partNumber;
      this.Description = description;
      this.Price = price;
      this.Quantity = quantity;
   }
}

使用示例

Invoice invoice = new Invoice("42-42-42", "Awesome Foo", 24, 42.42);

invoice.Quantity = 42;

Console.WriteLine("Part number : {0}", invoice.PartNumber);
Console.WriteLine("Description : {0}", invoice.Description);
Console.WriteLine("Price       : {0}", invoice.Price);
Console.WriteLine("Quantity    : {0}", invoice.Quantity);
Console.WriteLine("Total       : {0}", invoice.Total);
于 2009-09-28T18:21:08.100 回答
0

没有必要在同一类的方法中使用 getter(假设 getter 只检索私有成员变量值)。该类拥有所有成员变量的全部所有权,因此可以直接使用它们。

除非 getter 方法做一些复杂的事情,否则没有理由保护类不受自身影响。

于 2009-09-28T18:41:36.660 回答