我有下面的代碼控制檯應用程序:int字段的默認值爲0嗎?
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.當然,這隻有在Discou nt屬性已設置,但如果調用屬性Discount,則變量iGotChanged
應該返回1或2,但它似乎保持爲0.因爲它是int
類型,因此payPerViewDiscount
的默認值爲0?
+1謝謝 - 我認爲這可能是這種情況,但它似乎有點違背語言的強類型安全基礎 – whytheq
類型安全的嚴格性?怎麼來的 ? – Habib
我真的沒有正確表達它的意思......它看起來像一個非常嚴格的語言,但這種默認似乎違反嚴格? ...我有什麼意義 – whytheq