类方法中的多个参数

发布于 2024-08-05 13:20:21 字数 1612 浏览 6 评论 0原文

完全初学者的问题。我正在尝试为一个类创建一个方法,该方法根据 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

Total beginner question. I am attempting to create a method for a class that calculates the total price for an object based on int Quantity and decimal price. Both of these are private and have Instance Variable Property Assignments. I cannot figure out how to access them and compute on them when I am using two separate parameters in the method. The method in question is the GetInvoiceAmount. Any suggestions would be greatly appreciated

//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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如果没有你 2024-08-12 13:20:21

您在寻找这样的东西吗?

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 方法和 < code>invoiceAmount 字段,并且不再需要它们。

Are you looking for something like this?

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

Your current GetInvoiceAmount implementation sets the public field invoiceAmount so to use the current method you would need to do something like this:

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

which seems counter-intuitive since the method says that it is "getting" something.

For clarity's sake I would recommend that this be a property that is generated each time it is called (that is a property that has no backing field) like this:

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

Then you can remove the GetInvoiceAmount method and the invoiceAmount field as well as they would no longer be needed.

过去的过去 2024-08-12 13:20:21

那么下面的呢?我将属性设置器设为私有,以避免在对象构造之后修改值,但稍后可能会更改的数量除外(使用只读属性会更好,但它们首先出现在 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);

What about the following? I made the property setters private to avoid modifying the values after object construction except for the quantity that might change later (it would be even better to use read-only properties, but they first come in C# 4.0 and using read-only backing fields adds quite a bit of noise to the code). You can avoid checking for non-negative quantities by using an unsigned integer. The price gets already checked in the constructor. And finally the total is returned by a computed property.

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

USAGE EXAMPLE

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);
怕倦 2024-08-12 13:20:21

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

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

It's not necessary to use the getters within methods of the same class (assuming that the getters do nothing more that just retrieve the private member variable values). The class has total ownership of all the member variables, so it can use them directly.

Unless a getter method does something complicated, there is no reason to protect a class from itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文