JAVA28. 创建单例对象
描述
Singleton类是单例的,每次调用该类的getInstance()方法都将得到相同的实例,目前该类中这个方法尚未完成,请将其补充完整,使得main()函数中的判断返回真(不考虑线程安全)。输入描述
无输出描述
trueJava 解法, 执行用时: 7ms, 内存消耗: 9008KB, 提交时间: 2022-02-08
public class Main { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); } } class Singleton { private static Singleton instance; private Singleton() { } //write your code here...... public static Singleton getInstance() { return instance; } }
Java 解法, 执行用时: 7ms, 内存消耗: 9064KB, 提交时间: 2022-02-10
public class Main { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); } } class Singleton { private static Singleton instance; private Singleton() { } //write your code here...... public static Singleton getInstance() { return instance; } }
Java 解法, 执行用时: 7ms, 内存消耗: 9104KB, 提交时间: 2021-10-14
public class Main { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); } } class Singleton { private static Singleton instance; private Singleton() { } //write your code here...... public static Singleton getInstance() { return instance; } }
Java 解法, 执行用时: 7ms, 内存消耗: 9108KB, 提交时间: 2021-11-18
public class Main { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); } } class Singleton { private static Singleton instance; private Singleton() { } //write your code here...... public static Singleton getInstance(){ if(instance == null) instance = new Singleton(); return instance; } }
Java 解法, 执行用时: 7ms, 内存消耗: 9152KB, 提交时间: 2021-10-13
public class Main { public static void main(String[] args) { Singleton s1 = Singleton.getInstance(); Singleton s2 = Singleton.getInstance(); System.out.println(s1 == s2); } } class Singleton { private static Singleton instance; private Singleton() { } public static Singleton getInstance() { if (instance == null) { instance = new Singleton(); } return instance; } //write your code here...... }