打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
单例模式懒汉式\饿汉式
=================Student类==============
懒汉式:
package com.zx.cn;
public class Student {
 private Student(){
 }
 private static Student s = null;
 public synchronized static Student getStudent(){
  if(s == null){
   s = new Student();
  }
  return s;
 }
}
=============测试类==============
package com.zx.cn;
public class StudentDame {
 public static void main(String[] args) {
  Student s1 = Student.getStudent();
  Student s2 = Student.getStudent();
  System.out.println(s1 == s2);
  System.out.println(s1);
  System.out.println(s2);
 }
}
=======================Student类===============
饿汉式
package com.zx.cn;
public class Student {
 private Student(){
 }
 private static Student s = new Student();
 
 public static Student getStudent(){
  return s;
 }
}
================测试类============
package com.zx.cn;
public class StudentDame {
 public static void main(String[] args) {
  Student s1 = Student.getStudent();
  Student s2 = Student.getStudent();
  System.out.println(s1 == s2);
  System.out.println(s1);
  System.out.println(s2);
 }
}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java 设计模式
单例设计模式
枚举类型的单例模式(java)
单例模式
详解JAVA面向对象的设计模式 (一)、单例模式
面试高频-吃透单例设计模式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服