1

全くの初心者質問。int Quantity と小数点以下の価格に基づいてオブジェクトの合計価格を計算するクラスのメソッドを作成しようとしています。これらは両方とも非公開で、インスタンス変数のプロパティの割り当てがあります。メソッドで 2 つの個別のパラメーターを使用している場合、それらにアクセスして計算する方法がわかりません。問題のメソッドは 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実装では public フィールドが設定さ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 メソッドが何か複雑なことをしない限り、クラスをそれ自体から保護する理由はありません。

于 2009-09-28T18:41:36.660 に答える