0
使用單身懶惰,因爲我試圖做的比我想象的要困難。 但我想我已經做到了50%。我的問題是關於下一個代碼,爲什麼我需要通過Instanciation _I傳遞到'int函數',而不是通過_I傳遞到結構? 是否實施了單身權利,還是我不明白? 感謝帶嵌套類的C#單例函數和結構
代碼
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Example_Singleton.Singleton.Data.Act_Number + "\n");
Console.WriteLine(Example_Singleton.Singleton.Time.TLength_Form.Time50ms.ToString()+"\n");
Example_Singleton.Singleton.Time._IT.waitTimer(3000);
Console.WriteLine("done");
Console.ReadKey();
}
}
class Example_Singleton
{
public sealed class Singleton
{
private Singleton()
{
}//ctor singleton
public static Singleton _I { get { return Nested.instance; } }//instance called for singleton
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested() { }//ctor nested
internal static readonly Singleton instance = new Singleton();
}
public struct Data
{//base structure for data station
public static Int16 Act_Number = 352;
}
public sealed class Time/* Class Time for PSX*/
{
private Time()
{
}//ctor
public static Time _IT { get { return thisTime.instance; } }
private class thisTime
{
static thisTime() { }//ctor
internal static readonly Time instance = new Time();
}
internal void waitTimer(Int32 TimeLength) //Wait timer
{
System.Threading.Thread.Sleep(TimeLength);
}
internal struct User_Time
{
public const int RefreshData = 100;
public const int WaitSynchro = 10;
}
internal struct TLength_System //time length definitions for system time
{
public const double Time10ms = 10;
public const double Time50ms = 50;
}
internal struct TLength_Form //time length definitions for Form time
{
public const int Time10ms = 10;
public const int Time50ms = 50;
}
}
}
你到底想用這種單身模式做什麼?看起來你建立了一些非常複雜的東西,你可能不需要。 – Aboc
我想只有一大堆Formclass使用的嵌套類的一個實例。像時間一樣,我們並不需要很多先例。或者我會添加一個服務器類單例,其中只有一個實例方法讀取和寫入可以互相關聯,所以我確信一次只有一個調用者訪問服務器...就是這一點。 – Jablonovo