类方法中的多个参数
完全初学者的问题。我正在尝试为一个类创建一个方法,该方法根据 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在寻找这样的东西吗?
您当前的 GetInvoiceAmount 实现设置了公共字段 invoiceAmount ,因此要使用当前方法,您需要执行以下操作:
这似乎违反直觉,因为该方法说它是“得到”某物。
为了清楚起见,我建议这是每次调用时生成的属性(即没有支持字段的属性),如下所示:
然后您可以删除
GetInvoiceAmount
方法和 < code>invoiceAmount 字段,并且不再需要它们。Are you looking for something like this?
Your current
GetInvoiceAmount
implementation sets the public fieldinvoiceAmount
so to use the current method you would need to do something like this: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:
Then you can remove the
GetInvoiceAmount
method and theinvoiceAmount
field as well as they would no longer be needed.那么下面的呢?我将属性设置器设为私有,以避免在对象构造之后修改值,但稍后可能会更改的数量除外(使用只读属性会更好,但它们首先出现在 C# 4.0 中,并使用只读支持字段添加代码中有相当多的噪音)。您可以使用无符号整数来避免检查非负数。价格已在构造函数中检查。最后,总数由计算属性返回。
使用示例
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.
USAGE EXAMPLE
没有必要在同一类的方法中使用 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.