gpt4 book ai didi

java - 无法使用日历创建具有所需 API 的对象

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:51 25 4
gpt4 key购买 nike

我正在尝试使用所需的构造函数创建火车时刻表的多个对象,

public Station(String city, Calendar arrival, Calendar departure, int day)

但是,当我创建对象时,我无法传递到达和出发时间,因为 20:30 等被视为 int。我希望在构造函数中将 Calendar 更改为 int,但显然它必须使用 Calendar 来完成。

Station stop1= new  Station("Vancouver",null , 2030, 1)

这是我尝试创建的,但就像我说的,它不会接受参数。

很明显,我需要跳出框框思考并开始摆弄日历,但我不确定是否应该在下面的构造函数下这样做:

 public Station(String city, Calendar arrival, Calendar departure, int day){    
this.city=city;
this.arrival=arrival;
this.departure=departure;
this.day=day;
}

或者如果我必须创建一些新方法并将其全部结合在一起。在我的研究中,我发现了一种稍微改变了的方法,但我似乎仍然无法使用“到达”和“离开”这些参数将其实现到我的对象创建中。

private static Calendar getCalendar(int hour, int minute) {
Calendar c = Calendar.getInstance();
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute);
return c;
}

所以我想我想知道是否有人可以提供一些提示,为我指出其他事情的正确方向,也许我应该在网上查找。这是我作业中的最后一个也是最难的问题,我什至无法创建 10 个对象来启动火车时刻表。

最佳答案

I would love to just change Calendar to int in the constructor, but apparently it has to be done with Calendar.

我不确定你的意思到底是什么,但可以说,出于某种原因,你更喜欢保留构造函数现在的样子,那么你需要对已经发布的代码执行如下操作

Station stop1= new  Station("Vancouver",null , getCalendar(20, 30) , 1)

否则您可以按照其他答案中的建议进行操作

关于java - 无法使用日历创建具有所需 API 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37442188/

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