本帖最后由 李盈科 于 2012-1-8 10:39 编辑
为什么运行结果是 1 和0???- public class SingletonDemo
- {
- public static void main(String[] args)
- {
- Singleton singleton = Singleton.getInstance();
-
- System.out.println(singleton.number1);
- System.out.println(singleton.number2);
- }
- }
- class Singleton
- {
- private static Singleton singleton = new Singleton();
- public static int number1;
- public static int number2 = 0;
-
-
- private Singleton()
- {
- number1++;
- number2++;
- }
-
- public static Singleton getInstance()
- {
- return singleton;
- }
- }
复制代码 |