有这样一个类: class myTest {
private static myTest mt; static myTest() {
mt = new myTest();
}
}请问,myTest是怎么样在自己内部,用myTest自己定义字段的和对象实例化的?原理是怎么样的?
也许是很简单的问题,可是我没有搞懂这个究竟是这么一个形式。请大家帮忙解答一下,谢谢。
private static myTest mt; static myTest() {
mt = new myTest();
}
}请问,myTest是怎么样在自己内部,用myTest自己定义字段的和对象实例化的?原理是怎么样的?
也许是很简单的问题,可是我没有搞懂这个究竟是这么一个形式。请大家帮忙解答一下,谢谢。
但如果把static去掉就会有问题,不知道编译器会不会让它通过,就算通过了,实例化的时候也会有堆栈溢出的异常吧。
希望下面这段代码能帮你理解它的含义
using System;
using System.IO;
using System.Collections;public class MyClass
{
public static void Main()
{
//First
Singleton t1 = Singleton.Instance;
Singleton t2 = Singleton.Instance;
t1.t = "test";
Console.WriteLine(Object.ReferenceEquals(t1,t2)==true);
Console.WriteLine(t2.t);
Console.ReadLine();
//Second
Singleton2 t3 = Singleton2.Instance;
Singleton2 t4 = Singleton2.Instance;
t3.t = "test";
Console.WriteLine(Object.ReferenceEquals(t3,t4)==true);
Console.WriteLine(t4.t);
Console.ReadLine();
//Third
Singleton3 t5 = Singleton3.Instance;
Singleton3 t6 = Singleton3.Instance;
t5.t = "test";
Console.WriteLine(Object.ReferenceEquals(t3,t4)==true);
Console.WriteLine(t6.t);
Console.ReadLine();
Console.WriteLine(Singleton3.Instance);
Console.ReadLine();
//Forth
Singleton4 t7 = Singleton4.GetInstance(100,200);
Singleton4 t8 = Singleton4.GetInstance(200,300);
Console.WriteLine(Object.ReferenceEquals(t7,t7)==true);
Console.WriteLine(t7.x + "--" + t7.y);
Console.WriteLine(t8.x + "--" + t8.y);
Console.ReadLine();
}
}//Normal Singleton
class Singleton
{
private static Singleton instance;
private Singleton(){}
public static Singleton Instance
{
get
{
if(instance == null)
{
instance = new Singleton();
}
return instance;
}
}
private string m_t;
public string t
{
get{ return m_t; }
set{ m_t = value; }
}
}//Multi-thread Singleton
class Singleton2
{
//限定一个对象可被外部进程(操作系统、硬件或并发线程等)改变
private static volatile Singleton2 instance = null;
private static object lockHelper = new Object();
private Singleton2() {}
public static Singleton2 Instance
{
get
{
if(instance == null)
{
lock (lockHelper)
{
if(instance == null)
{
instance = new Singleton2();
}
}
}
return instance;
}
}
private string m_t;
public string t
{
get{ return m_t; }
set{ m_t = value; }
}
}//Static Singleton
class Singleton3
{
//Create a readonly instance
public static readonly Singleton3 Instance;
static Singleton3()
{
Instance = new Singleton3();
}
private Singleton3() { }
private string m_t;
public string t
{
get{ return m_t; }
set{ m_t = value; }
}
}//Normal Singleton
class Singleton4
{
private static Singleton4 instance;
public int x;
public int y;
private Singleton4(int x,int y)
{
this.x = x;
this.y = y;
}
public static Singleton4 GetInstance(int x,int y)
{
if(instance == null)
{
instance = new Singleton4(x,y);
}
else //Overwrite the property,but just one object
{
instance.x = x;
instance.y = y;
}
return instance;
}
}