• 设计模式单例模式


    类图

     

    源码

    package com.test;
    
    public class StuNo {
        private static StuNo instance=null;
        private String no;
        
        private StuNo()
        {    
        }
        
        public static StuNo getInstance()
        {
            if(instance==null)
            {
                System.out.println("入学,分配学号!");
                instance=new StuNo();
                instance.setStuNo("20193922");        
            }
            else
            {
                System.out.println("返校,获取旧号码!");  
            }
            return instance;
        }
        
        private void setStuNo(String no)
        {
            this.no=no;
        }
        
        public String getStuNo()
        {
            return this.no;
        }
    }
    
    
    Client.java
    package com.test;
    
    public class Client {
        public static void main(String a[])
        {
            StuNo no1,no2;
           no1=StuNo.getInstance();
           no2=StuNo.getInstance();
           System.out.println("学号是否一致:" + (no1==no2));
           
           String str1,str2;
           str1=no1.getStuNo();
           str2=no1.getStuNo();
           System.out.println("第一次学号:" + str1);
           System.out.println("第二次学号:" + str2);
           System.out.println("内容是否相等:" + str1.equalsIgnoreCase(str2));
           System.out.println("是否是相同对象:" + (str1==str2));
        }
    }

    测试截图

     

     

     

  • 相关阅读:
    还需要做恰当的解读,此时你可能需要一些书:
    创业公司的架构演进史
    任务调度平台Cuckoo-Schedule
    ORACLE中死锁
    Action的模型绑定
    三次握手、四次握手、backlog
    Django框架
    扩展BootstrapTable的treegrid功能
    Identity Service
    Linux权限
  • 原文地址:https://www.cnblogs.com/lx06/p/15688365.html
Copyright © 2020-2023  润新知