java中main方法写在不同类中的问题

dpual 发布于 2012/07/06 15:49
阅读 634
收藏 0
public class Singleton
{
 private static Singleton single;
 
 public static Singleton getInstance()
 {
  if(single == null)
  {
   single = new Singleton();
  }
  return single;
  
 }
 
}

public class Test
{
 public static void main(String[] args)
 {
  Singleton single1 = Singleton.getInstance();
  Singleton single2 = Singleton.getInstance();
  System.out.println(single1);
  System.out.println(single2);
 }
}

与下面程序的区别:

public class Singleton
{
 private static Singleton single;
 
 public static Singleton getInstance()
 {
  if(single == null)
  {
   single = new Singleton();
  }
  return single;
  
 }

 public static void main(String[] args)
 {
  Singleton single1 = Singleton.getInstance();
  Singleton single2 = Singleton.getInstance();
  System.out.println(single1);
  System.out.println(single2);
 }
}

请问这两个程序有什么区别??

加载中
0
红薯
红薯
没什么区别 :) 看你的个人喜好,第二种只需要一个类,更简单些。
返回顶部
顶部