大家都知道,DateFormat 是一个抽象类,可是为什么这个抽象类里竟然有创建实例的方法:public static final DateFormat getInstance()这个是什么原因呢?
不知道这个方法的源码里是怎么实现的啊?
它到底是创建的谁的实例呢?

解决方案 »

  1.   

    他创建的是SimpleDateFormat的实例 ...
      

  2.   

    public final static DateFormat getInstance() {
        return getDateTimeInstance(SHORT, SHORT);
    }public final static DateFormat getDateTimeInstance(int dateStyle,int timeStyle) {
        return get(timeStyle, dateStyle, 3, Locale.getDefault());
    }private static DateFormat get(int timeStyle, int dateStyle,int flags, Locale loc) {
        if ((flags & 1) != 0) {
            if (timeStyle < 0 || timeStyle > 3) {
                throw new IllegalArgumentException("Illegal time style " + timeStyle);
            }
        } else {
            timeStyle = -1;
        }
        if ((flags & 2) != 0) {
            if (dateStyle < 0 || dateStyle > 3) {
                throw new IllegalArgumentException("Illegal date style " + dateStyle);
            }
         } else {
            dateStyle = -1;
         }
         try {
            return new SimpleDateFormat(timeStyle, dateStyle, loc);     } catch (MissingResourceException e) {
            return new SimpleDateFormat("M/d/yy h:mm a");
         }
     }
      

  3.   

    上面的代码少个常量
    public static final int SHORT = 3;
      

  4.   

    我的Eclipse打开DateFormat.class文件后,就能看到源码~
    不知道是不是装了什么插件,我用的是别人给的Eclipse