2015年11月9日 星期一

C7_2 測試靜態成員與非靜態成員

using System;
namespace C7_2
{
    public class Test
    {
        public int x;
        public static int y; //宣告一個靜態變數
        public void F()
        {
            x = 1; //正確,相等於this.x = 1
            y = 1; //正確,相等於Text.y = 1
        }
        public static void G() //宣告一個靜態方法
        {
            //x =1; //錯誤,不能存取this.x
            y = 1; //正確,相等於Test.y = 1
        }
       
    }
    public class Program
    {
        public static void Main()
        {
            Test test = new Test();
            test.F(); //正確
            //test.G();
            //錯誤,無法使用實力參考來存取成員,"C7_2.Test.G()"請改用類型名來限定他
            Test.G();
        }
    }
}

沒有留言: