简体   繁体   English

Java从另一个构造函数调用构造函数而不立即拥有参数

[英]Java Call a Constructor from Another Constructor Without Immediately Having the Parameters

Is there is any way to call a constructor from another constructor without immediately having the parameters for it? 有没有办法从另一个构造函数调用构造函数而不立即拥有它的参数?

I ran into this problem while trying to create a constructor for my SimpleDate class that took in a millis time parameter and used another constructor to create the class (code below). 我试图为我的SimpleDate类创建一个构造函数时遇到了这个问题,该构造函数接受了millis time参数并使用了另一个构造函数来创建类(下面的代码)。 The problem that I ran into was that the constructor call has to be on the first line but I don't really see anyway to get a Calendar instance with the right time without first setting the time in milliseconds on a previous line. 我遇到的问题是构造函数调用必须在第一行,但我实际上并没有真正看到正确的时间获取Calendar实例而没有先在前一行上设置时间(以毫秒为单位)。 I don't see how do that on one line, because setTimeInMillis is a void method and I don't think it possible to return the value after invoking a method (if it is than i would very much like to know how as well). 我不知道在一行上怎么做,因为setTimeInMillis是一个void方法,我不认为在调用方法后可以返回值(如果它比我非常想知道如何) 。 I realize this whole thing is not really completely necessary but i do want to know if it is possible and if it is, how I would do it. 我意识到这一切并非完全是必要的,但我确实想知道它是否可能,如果可能,我将如何做到。

        public SimpleDate(long timeMillis) {
            this(Calendar.getInstance().setTimeInMillis(timeMillis));//Obviously this doesn't work because setTimeInMillis is a void method
        }

        public SimpleDate(Calendar calendar) {
            this.year = calendar.get(Calendar.YEAR);
            this.month = calendar.get(Calendar.MONTH) + 1;
            this.day = calendar.get(Calendar.DAY_OF_MONTH);

            this.hour = calendar.get(Calendar.HOUR_OF_DAY);
            this.minute = calendar.get(Calendar.MINUTE);
            this.second = calendar.get(Calendar.SECOND);
        }

You could do this by calling a static method on the class to perform the necessary conversion for you. 您可以通过在类上调用静态方法来执行此操作,以便为您执行必要的转换。

protected static Calendar getCalendarForMillis(long millis) {
    Calender ret = Calendar.getInstance();
    ret.setTimeInMillis(millis);
    return ret;
}

public SimpleDate(long millis) {
    this(getCalendarForMillis(millis));
}

The answer to your question is no: you can't call another constructor later in the initialization if the parameters aren't ready right at the top of the constructor. 您的问题的答案是否定的:如果参数未在构造函数的顶部准备好,则无法在初始化后调用另一个构造函数。 That's why you have to do some static method workaround. 这就是为什么你必须做一些静态方法的解决方法。

What you want is just reuse of the code which creates your fields from a Calendar instance, with the ability to convert one constructor's long millis argument to a Calendar instance first. 你想要的只是重用从Calendar实例创建字段的代码,能够首先将一个构造函数的long millis参数转换为Calendar实例。

If you put that Calendar -> your fields code directly into one constructor, it means that to reuse it you have to call that constructor on the first line of the other constructor. 如果你把那个Calendar - > your fields代码直接放到一个构造函数中,这意味着要重用它,你必须在另一个构造函数的第一行调用该构造函数。 This is a bit clumsy because it forces you to use a separate static millis -> Calendar method, which probably won't be used elsewhere. 这有点笨拙,因为它会强制您使用单独的静态millis - > Calendar方法,这可能不会在别处使用。

A more flexible, arguably cleaner solution is to just put that Calendar -> your fields code in a separate method, and then call it from both constructors: 一个更灵活,可以说更清晰的解决方案是将Calendar - > your fields代码放在一个单独的方法中,然后从两个构造函数中调用它:

public SimpleDate(Calendar calendar) {
  setDateFields(calendar);        
}

public SimpleDate(long millis) {
  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(millis);
  setDateFields(calendar);      
}

private void setDateFields(Calendar calendar) {
  this.year = calendar.get(Calendar.YEAR);
  this.month = calendar.get(Calendar.MONTH);
  this.day = calendar.get(Calendar.DAY_OF_MONTH);
  this.hour = calendar.get(Calendar.HOUR_OF_DAY);
  this.minute = calendar.get(Calendar.MINUTE);
  this.second = calendar.get(Calendar.SECOND);
}

This way, you can do setup, or anything else you like, before calling the method in either constructor, which I believe is what you really wanted. 这样,你可以在构造函数中调用方法之前进行设置或其他任何你喜欢的操作,我相信这是你真正想要的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM