C#語言基礎(三)

  
18、類屬性
using System;
using System.Collections.Generic;
using System.Text;
//類屬性,類TimePeriod存儲了一個時間段。類內部以秒爲單位存儲時間,但提供一個名稱爲Hours的屬性,
//它允許客戶端指定以小時爲單位的時間。Hours屬性的訪問器執行小時和秒之間的轉換。
namespace ClassPropertyTest
{
    class ClassPropertyTest
    {
        static void Main(string[] args)
        {
            TimePeriod t=new TimePeriod();
            t.Hours=24;//設置時間屬性
            Console.WriteLine("Time in hours:"+t.Hours);//獲取時間屬性
            SampleRefType rt = new SampleRefType();
            rt.value = 44;
            ModifyObject(rt);
            System.Console.WriteLine(rt.value);
        }
        class TimePeriod
        {
            private double hours;
            public double Hours
            {
                get { return hours / 3600; }//返回屬性值
                set { hours = value * 3600; }//分配新值
            }
        }
        public class SampleRefType
        {
            public int value;
        }       
        static void ModifyObject(SampleRefType obj)
        {
            obj.value = 33;
        }
    }
}
19、索引器
using System;
using System.Collections.Generic;
using System.Text;
//索引器,代碼中定義了一個泛型類,併爲其提供了一個簡單的get和set訪問器方法。
namespace SuoyinqiTest
{
    class SampleCollection<T>
    {
        private T[] arr = new T[100];
        public T this[int i]
        {
            get
            {
                return arr[i];
            }
            set
            {
                arr[i] = value;
            }
        }
    }
    class Suoyinqi
    {
        static void Main(string[] args)
        {
            SampleCollection<string> stringCollection = new SampleCollection<string>();
            SampleCollection<int> intCollection = new SampleCollection<int>();
            stringCollection[0] = "Hello,Word";
            intCollection[0] = 12345;
            System.Console.WriteLine(stringCollection[0]);
            System.Console.WriteLine(intCollection[0]);
        }
    }
}
20、構造函數
using System;
using System.Collections.Generic;
using System.Text;
//構造函數,代碼中定義了一個構造函數Taxi類,使用new運算符來實例化該類。
namespace GouZTestTaxi
{
    class TestTaxi
    {
        static void Main(string[] args)
        {
            Taxi t = new Taxi();
            Console.WriteLine(t.isInitialized);
        }
    }
 
    public class Taxi
    {
        public bool isInitialized;
        public Taxi()
        {
            isInitialized = true;
        }
    }
}
21、實例構造函數
using System;
using System.Collections.Generic;
using System.Text;
//實例構造函數用於創建和初始化實例。創建新對象時將調用類構造函數,代碼中演示包含兩個類構造函數的類;
//一個類構造函數沒有參數,另一個類構造函數帶有兩個參數。
namespace GouZTestConstruct
{
    class TestConstruct
    {
        static void Main(string[] args)
        {
            CoOrds p1 = new CoOrds();
            CoOrds p2 = new CoOrds(5, 3);
            Console.WriteLine("CoOrds #1 at {0}", p1);
            Console.WriteLine("CoOrds #2 at {0}", p2);
        }
    }
    class CoOrds
    {
        public int x, y;
        //默認構造函數
        public CoOrds()
        {
            x = 0;
            y = 0;
        }
        //構造函數中帶有兩個參數
        public CoOrds(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        //重寫ToString方法
        public override string ToString()
        {
            return (System.String.Format("({0},{1})", x, y));
        }
    }
 
}
22、私有構造函數
using System;
using System.Collections.Generic;
using System.Text;
//私有構造函數是一種特殊的實例構造函數。它通常用在只包含靜態成員的類中。
//如果類具有一個或多個私有構造函數而沒有公共構造函數,則不允許其他類(除了嵌套類)創建該類的實例。
namespace GouZTestCouter
{
    class TestCounter
    {
        static void Main(string[] args)
        {
            Counter.currentCount = 100;
            Counter.IncrementCount();
            Console.WriteLine("New count:{0}", Counter.currentCount);
        }
    }
    public class Counter
    {
        private Counter() { }
        public static int currentCount;
        public static int IncrementCount()
        {
            return ++currentCount;
        }
    }
}
23、靜態構造函數
using System;
using System.Collections.Generic;
using System.Text;
//靜態構造函數,代碼中類Bus有一個靜態構造函數和一個靜態成員Drive()。當調用Drive()時將調用靜態構造函數來初始化類。
namespace GouZTestBus
{
    class TestBus
    {
        static void Main(string[] args)
        {
            Bus.Drive();
        }
    }
    public class Bus
    {
        //靜態構造函數
        static Bus()
        {
            System.Console.WriteLine("The static constructor invoked.");
        }
        public static void Drive()
        {
            System.Console.WriteLine("The Drive method invoked.");
        }
    }
}
24、析構函數
using System;
using System.Collections.Generic;
using System.Text;
//析構函數用來執行對象的終止操作。
namespace GouZTestDestructors
{
    class TestDestructors
    {
        static void Main(string[] args)
        {
            Third t = new Third();
        }
    }
    class First
    {
        ~First()
        {
            System.Console.WriteLine("First's destructor is called.");
        }
    }
    class Second : First
    {
        ~Second()
        {
            System.Console.WriteLine("Second's destructor is called.");
        }
    }
    class Third : Second
    {
        ~Third()
        {
            System.Console.WriteLine("Third's destructor is called.");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章