类方法中的多个参数

时间:2009-09-28 17:45:36

标签: c# encapsulation

总的初学者问题。我正在尝试为类创建一个方法,该类根据int Quantity和decimal price计算对象的总价。这两个都是私有的,并具有实例变量属性分配。当我在方法中使用两个单独的参数时,我无法弄清楚如何访问它们并对它们进行计算。有问题的方法是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

3 个答案:

答案 0 :(得分: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字段,以及不再需要它们。

答案 1 :(得分: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);

答案 2 :(得分:0)

没有必要在同一个类的方法中使用getter(假设getter不做任何更多只是检索私有成员变量值)。该类具有所有成员变量的完全所有权,因此可以直接使用它们。

除非getter方法复杂化,否则没有理由保护类本身。