使用 C# 在子類中重寫父類的虛屬性時,發現了了一處 IDE 的異常,監視器中顯示的值與實際變量值不一致,不知道是否是它的 BUG , 05 和 08 種均存在該問題。
代碼如下:紅色部分是出現異常的地方
public partial class Form1 : Form
{
public Form1 ()
{
InitializeComponent ();
}
private void button1_Click (object sender , EventArgs e )
{
SunGame pGame = new SunGame ();
pGame .Test ();
}
}
public class Game
{
public virtual Boolean IsClosed
{
get
{
return false ;
}
}
};
public class SunGame : Game
{
public override Boolean IsClosed
{
get
{
return true ;
}
}
public void Test ()
{
// 在這裏輸出都是正常的, 但只能提示和監視結果不對
Console .WriteLine (" 父類:" + base .IsClosed );
Console .WriteLine (" 子類:" + IsClosed );
上面兩行代碼分別會輸出父類中的false 和子類中的true ,這裏是正確的,但是將這兩行代碼加入監視器中會發現結果都是true, 鼠標智能提示也是如此,如下圖:
}
}