<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 單例模式 在實際的開發中,絕大部分的服務性質的類都會設計成單例模式 所謂的單例模式,就是類只有一個對象,外部要使用該類的對象,通過調用一個類方法實現。 ## 單例模式特點 * 1、單例類只能有一個實例。 * 2、單例類必須自己創建自己的唯一實例。 * 3、單例類必須給所有其他對象提供這一實例。 ## 單例模式實現 關鍵代碼:保證構造函數是私有的。 ~~~ /** * 餓漢式單例模式 * @author lzq31 * */ public class Service2 { private static Service2 service = new Service2(); private Service2() { } public static Service2 getInstance() { return service; } } ~~~ ~~~ /** * 懶漢式單例模式 * @author lzq31 * */ public class Service { private static Service service; private Service() { } public static Service getInstance() { if (null == service) { service = new Service(); } return service; } } ~~~ ## 單例模式的檢測 ~~~ //當Demo為單例模式時 public class Demo { private static Demo demo = new Demo(); private Demo() {} public static Demo getInstance() { return demo; } public int a = 1; } public class Demo2 { public static void main(String[] args) { Demo demo = Demo.getInstance(); Demo demo2 = Demo.getInstance(); demo.a = demo.a + 1; System.out.println(demo.a); System.out.println(demo2.a); } } //當Demo不為單例模式的時候 public class Demo { public int a = 1; } public class Demo2 { public static void main(String[] args) { Demo demo = new Demo(); Demo demo2 = new Demo(); demo.a = demo.a + 1; System.out.println(demo.a); System.out.println(demo2.a); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看