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

发布于 2024-11-07 20:58:36 字数 214 浏览 0 评论 0原文

我只是想知道......

为什么我只能有一个 Calendar 对象的实例。它有单身的理由吗?

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

I was just wondering...

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巨坚强 2024-11-14 20:58:36

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

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

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

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

药祭#氼 2024-11-14 20:58:36

它不是单例。

这是:

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);
    }

输出:(

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]

如您所见,这是不同的)

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

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

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();
}
只是我以为 2024-11-14 20:58:36

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

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

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

getInstance() returns a new instance each time.

殤城〤 2024-11-14 20:58:36

您可以根据需要拥有尽可能多的 Calendar 实例...以它是一个抽象类为模,因此您正在谈论 Calendar 的子类的实例。

也许您认为 getInstance() 方法返回一个单例对象?事实并非如此。每次调用它时它都会创建并返回一个新对象。

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

You can have as many instances of Calendar as you want ... modulo that it is an abstract class, so you are talking about of instances of child classes of 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.

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文