我有一个带有以下代码的控制台应用程序:
using System;
namespace HeadfirstPage210bill
{
class Program
{
static void Main(string[] args)
{
CableBill myBill = new CableBill(4);
Console.WriteLine(myBill.iGotChanged);
Console.WriteLine(myBill.CalculateAmount(7).ToString("£##,#0.00"));
Console.WriteLine("Press enter to exit");
Console.WriteLine(myBill.iGotChanged);
Console.Read();
}
}
}
CableBill.cs 类如下:
using System;
namespace HeadfirstPage210bill
{
class CableBill
{
private int rentalFee;
public CableBill(int rentalFee) {
iGotChanged = 0;
this.rentalFee = rentalFee;
discount = false;
}
public int iGotChanged = 0;
private int payPerViewDiscount;
private bool discount;
public bool Discount {
set {
discount = value;
if (discount) {
payPerViewDiscount = 2;
iGotChanged = 1;
} else {
payPerViewDiscount = 0;
iGotChanged = 2;
}
}
}
public int CalculateAmount(int payPerViewMoviesOrdered) {
return (rentalFee - payPerViewDiscount) * payPerViewMoviesOrdered;
}
}
}
控制台返回以下内容:
我看不到什么时候payPerViewDiscount
设置为 0。当然这只能在设置 Discount 属性时发生,但如果调用属性 Discount 则变量iGotChanged
应该返回 1 或 2,但它似乎保持在 0。因为它类型int
是否有默认值payPerViewDiscount
0?