gpt4 book ai didi

java - 公历 : Confusion with calling the method on the object

转载 作者:行者123 更新时间:2023-12-01 09:15:57 26 4
gpt4 key购买 nike

下面的程序是教程的一部分。我在这里有一些困惑包含以下几行:

     GregorianCalendar gCal = new GregorianCalendar();
gCal.add(GregorianCalendar.YEAR, 1);
this.expiryDate = gCal.getTime();

上面的代码不应该是这样的吗:

GregorianCalendar gCal = new GregorianCalendar();
SomeDatatype variable = gCal.add(GregorianCalendar.YEAR, 1);
this.expiryDate = variable.getTime();

以下是原程序部分:

package models;
import java.util.Date;
import java.util.GregorianCalendar;

import utilities.GenderType;


public class Customer {

private String title;
private String firstName;
private String surname;
private String address;
private String phoneNumber;
private String email;
private int customerNumber;
private GenderType gender;
private boolean isvalid;
private Date expiryDate;

public Customer(String title, String firstName, String surname, String address,
String phoneNumber, String email, int customerNumber, GenderType gender) {

setName(title, firstName, surname);
this.address = address;
this.phoneNumber=phoneNumber;
this.email = email;
this.customerNumber = customerNumber;
this.gender = gender;
this.isvalid = true;
GregorianCalendar gCal = new GregorianCalendar();
gCal.add(GregorianCalendar.YEAR, 1);
this.expiryDate = gCal.getTime();
}
}

最佳答案

GregorianCalendar 类中,add() 方法返回 void。

这样做的原因是它对 GregorianCalendar 的内部数据执行操作。它将定义的时间部分添加到其中。

您不需要从 add() 方法返回任何内容。同一个对象在调用后已经更新了,因此,您只需获取它包含的新时间值即可。

参见GregorianCalendar add() method引用。

关于java - 公历 : Confusion with calling the method on the object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40546129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com