简体   繁体   English

为什么Calendar类没有公共构造函数?

[英]Why does the Calendar class not have a public constructor?

Will new Calendar() have any difference from Calendar.getInstance() ? new Calendar()有任何差别Calendar.getInstance()

The question is just as simple as it is........... Since the system refuse to post it, I just copy some nonsense here. 这个问题就这么简单...........由于系统拒绝发布它,因此我在这里复制了一些废话。

private static Calendar calendar = Calendar.getInstance();
public static int getCalendar(long time, int calendarConst) {
    calendar.setTimeInMillis(time);
    return calendar.get(calendarConst);
}

I think the answer is simple Calendar is an Abstract class, so we cant create an instance of it. 我认为答案很简单Calendar是一个Abstract类,因此我们无法创建它的实例。 Now when you call Calendar.getInstance then GregorianCalendar instance is created. 现在,当您调用Calendar.getInstance时,将创建GregorianCalendar实例。

There is a detailed account of the difference between constructors and static factory methods in Effective Java second edition, which is well worth a read. 在Effective Java第二版中详细说明了构造函数和静态工厂方法之间的区别,非常值得一读。

The main difference here is in what the two return: 这里的主要区别在于两者的返回值:

  • The constructor always returns a non-null instance of the specific class on which the constructor is invoked, or it throws an exception and you don't get a reference to the part-constructed instance; 构造函数始终返回在其上调用构造函数的特定类的非null实例,否则它将引发异常,并且您不会获得对部分构造的实例的引用;
  • A static factory method can return a subclass or null (or it throws an exception). 静态工厂方法可以返回子类或null(或者引发异常)。

The latter case is exactly what Calendar.getInstance() does: you clearly don't get back an instance of Calendar itself, since it is abstract, but instead you might get a GregorianCalendar , JapaneseImperialCalendar etc. 后一种情况正是Calendar.getInstance()所做的:由于它本身是抽象的,因此您显然不会返回Calendar本身的实例,但是相反,您可能会得到GregorianCalendarJapaneseImperialCalendar等。

This decouples Calendar 's implementation from the client code: you can make changes to Calendar without the client needing to make changes. 这使Calendar的实现与客户端代码脱钩:您可以对Calendar进行更改,而无需客户端进行更改。

When you do new Calendar() , it creates new calendar instance from the default Calendar instance. 当您执行new Calendar() ,它将根据默认的Calendar实例创建新的日历实例。 Sometimes creating an instance via constructor is not good as creating from Factory methods. 有时通过构造函数创建实例不如从Factory方法创建实例。 That's why it uses Factory methods to create an instance. 这就是为什么它使用Factory方法创建实例的原因。

And, 和,

Calendar.getInstance() is a native method, and note down native methods don't have body/implementation. Calendar.getInstance()是本机方法, 请注意本机方法没有正文/实现。 They have implemented already from the system. 他们已经从系统中实现了。

It writes here: https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html . 它写在这里: https : //www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

And, as explained in above answers already, the calendar instance is created by calling Calendar class. 并且,正如上面答案中已经解释的那样,日历实例是通过调用Calendar类创建的。 Static Factory design pattern to create calendar instance. 用于创建日历实例的静态Factory设计模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么只有使用 static 方法的 class 会出现错误:“Add a private constructor to hide the implicit public one.”? - Why does only the class with the static method have the error: “Add a private constructor to hide the implicit public one.”? 为什么Java类构造函数必须是公共的或受保护的,以便将类扩展到其包之​​外? - Why does a Java constructor have to be public or protected for a class to be extended outside its package? Java Pattern类没有公共构造函数,为什么? - Java Pattern class doesn't have a public constructor, why? 为什么java.lang.reflect.Modifier具有公共构造函数? - Why does java.lang.reflect.Modifier have a public constructor? 为什么 Boolean 对象在 Java 中有一个公共构造函数? - Why does the Boolean object have a public constructor in Java? 一个类可以同时具有公共和私有构造函数吗? - Can a class have both public and private constructor? 为什么子类必须在超类中调用no args构造函数? - why does the subclass have to invoke the no args constructor in the super class? 既然抽象的 class 可以有一个公共的构造函数,为什么我们不能实例化它呢? - Since an abstract class can have a public constructor, why can't we instantiate it? 为什么类实例本身没有公共构造函数? - Why class instance itself has no public constructor? 为什么应该在javabean类中提供公共构造函数 - why public constructor should be provided in javabean class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM