I have a Console app with the following code:
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 呢?