java 为什么我只能有一个 Calendar 对象实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6055112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:09:11  来源:igfitidea点击:

Why can i have only one instance of Calendar object

javacalendar

提问by Gabriel

I was just wondering...

我只是想知道...

why can i have only one instance of Calendar object. Is there a reason for it to be a singleton?

为什么我只能有一个 Calendar 对象实例。有没有理由让它成为单身人士?

I have tried to read the documentationbut they didn't mention why this is needed. And a quick google search didn't give me any answers.

我试图阅读文档,但他们没有提到为什么需要这样做。快速的谷歌搜索没有给我任何答案。

回答by Codemwnci

Calendar is not a singleton, it is an abstract class. The getInstancemethod is a Factory method that returns a concrete implementation of the Calendar class.

Calendar 不是单例,它是一个抽象类。该getInstance方法是一个工厂方法,它返回 Calendar 类的具体实现。

Search Google for java.util.Calendar source code, and you will see how it works.

在 Google 上搜索 java.util.Calendar 源代码,您将看到它是如何工作的。

回答by MByD

It is not singleton.

它不是单例。

This:

这:

public static void main(String args[]) {
        Calendar c1, c2;
        c1 = Calendar.getInstance();
        c2 = Calendar.getInstance();
        c1.add(Calendar.MONTH, 1);
        System.out.println(c1);
        System.out.println(c2);
    }

Outputs:

输出:

java.util.GregorianCalendar[time=?,areFieldsSet=false,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=5,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=139,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=21,SECOND=27,MILLISECOND=839,ZONE_OFFSET=7200000,DST_OFFSET=3600000]
java.util.GregorianCalendar[time=1305789687839,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Jerusalem",offset=7200000,dstSavings=3600000,useDaylight=true,transitions=143,lastRule=java.util.SimpleTimeZone[id=Asia/Jerusalem,offset=7200000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=26,startDayOfWeek=6,startTime=7200000,startTimeMode=0,endMode=1,endMonth=8,endDay=13,endDayOfWeek=0,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2011,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=3,DAY_OF_MONTH=19,DAY_OF_YEAR=139,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=21,SECOND=27,MILLISECOND=839,ZONE_OFFSET=7200000,DST_OFFSET=3600000]

(Which is different as you can see)

(正如你所见,这是不同的)

BTW, quick search for source code returns:

顺便说一句,快速搜索源代码返回

public static synchronized Calendar getInstance() {
       return new GregorianCalendar();
}

回答by Joachim Sauer

Did you think that it is a singleton because it has a getInstance()method? That's not the case!

你认为它是单例是因为它有getInstance()方法吗?事实并非如此!

getInstance()returns a new instance each time.

getInstance()每次返回一个新实例。

回答by Stephen C

You can have as many instances of Calendaras you want ... modulo that it is an abstract class, so you are talking about of instances of child classesof Calendar.

您可以拥有任意数量的实例Calendar...取模它是一个抽象类,因此您正在谈论Calendar的子类的实例。

Perhaps you think that the getInstance()method returns a singleton object? It doesn't. It creates and returns a new object each time you call it.

也许您认为该getInstance()方法返回一个单例对象?它没有。每次调用它时,它都会创建并返回一个新对象。

(The javadoc doesn't explicitly state that the calendar is not a singleton, but it says "The Calendar returned is based on the current time ...". That implies that it is returning a new object each time ... because the current time keeps changing. And anyway, that's what the method does if you'd care to look at the source code.)

(javadoc 没有明确说明日历不是单例,但它说“返回的日历基于当前时间......”。这意味着它每次都返回一个新对象......因为当前时间不断变化。无论如何,如果您想查看源代码,这就是该方法所做的。)