首先是一些基础工作,这里是构造函数im
// user inputs a value say 4000 seconds, and then constructor will then convert that into hours, minutes and seconds
public Time(int inTttlSecs)
{
// compute the hours, minutes, seconds
// from the total seconds.
this.Hours = inTttlSecs/ 3600;
this.Minutes = (inTttlSecs - (this.Hours * 3600)) / 60;
this.Seconds = (inTttlSecs - (this.Hours * 3600) -
(this.Minutes * 60));
}
此构造函数以及用于这3个实例变量的适当的get和set方法用于创建时间对象
现在的问题是我想创建一个Time对象
使用此方法并以总秒数返回此时间对象的int类型
@return int this Time object in seconds
public int getTotalSecs()
{
// here i want to return type int this Time object total seconds
return 0;
}
问题是我可以创建一个时间对象
Time totalSecs
但我不知道如何使它输入int或返回总秒数
有任何想法吗?
或者如果您无法回答,请向我指出正确的方向,或在我迷路时向我提示
//回应以下答案我正在使用类时间作为另一个类中的实例变量对象,因此我需要将其作为新对象返回
最佳答案
如果您认为需要返回原材料,还应该返回totalSecs,为什么不简单地使其与Times,Minutes和Seconds一起成为Time类的成员变量,然后像这样返回它:
public int getTotalSecs()
{
return this.totalSecs;
}