ST.cs 773 B

123456789101112131415161718192021222324252627282930
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace LearnStorage
  5. {
  6. /// <summary>
  7. /// 如果从开始至终都没有使用过这个实例,则会造成内存的浪费
  8. /// </summary>
  9. public class ST
  10. {
  11. private static ST instance;
  12. private ST() // 不加这句构造函数将为public // 可以从外部new
  13. {
  14. instance = new ST(); // 将实例化放在私有构造函数中 // 饿汉式 // 不管用不用立刻构造一个
  15. }
  16. public static ST GetST()
  17. {
  18. // if (instance == null) // 懒汉式 // 不get不生成 // 多线程同时访问可能造成实例化多个
  19. // {
  20. // instance = new ST();
  21. // }
  22. return instance;
  23. }
  24. }
  25. }
  26. // private ST st = ST.GetST(); // 调用