添加与其他构造函数类似的构造函数意味着什么?
添加两个类似于 c 和 d 部分中描述的 set Time 方法的构造函数。
c 部分:编写一个方法 setTime(hour, 分钟),如果给定值有效则设置时间。
public void SetTime(int newHour, int newMinute)
{
if (hourIsValid = true)
hour = newHour;
if (minuteIsValid = true)
minute = newMinute;
}
d 部分:编写另一个方法 setTime(hour,minute,isAm),用于在给定值有效的情况下设置时间。给定的小时应在 1 到 12 的范围内。如果时间是上午时间,则参数 isAM 为 true,否则为 false。
public void SetTime(int newHour, int newMinute, boolean isAM)
{
if (hour >=0 && hour < 12)
{ isAM = true;
hour = newHour;}
if (minuteIsValid = true)
minute = newMinute;
if (isAM = true)
System.out.println ( hour + "a.m");
else
nightHour = hour % 12;
System.out.println( nightHour + "p.m");
}
这就是我到目前为止制作的内容,类似的要求制作什么?我知道它的意思类似,但是它的意思是像 C 部分一样只是两个单独的像 SetHour 和 SetMinute 吗?
Add two more constructors that are analogous to the set Time methods described in parts c and d.
Part c: Write a method setTime(hour, minute) that sets time if the given values are valid.
public void SetTime(int newHour, int newMinute)
{
if (hourIsValid = true)
hour = newHour;
if (minuteIsValid = true)
minute = newMinute;
}
Part d: Write another method setTime(hour,minute,isAm) that sets the time if the given values are valid. The given hour should be in the range of 1 to 12. The parameter isAM is true if the time is an a.m time and false otherwise.
public void SetTime(int newHour, int newMinute, boolean isAM)
{
if (hour >=0 && hour < 12)
{ isAM = true;
hour = newHour;}
if (minuteIsValid = true)
minute = newMinute;
if (isAM = true)
System.out.println ( hour + "a.m");
else
nightHour = hour % 12;
System.out.println( nightHour + "p.m");
}
That is what i produced so far, what is being asked to be produced by analogous? I know it means similar but w does it mean like for part C just the two separate like SetHour and SetMinute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每个类可以有多个构造函数,每个构造函数可以获取不同的变量。
对于本作业,您应该添加获取类似于 setter 函数的值的构造函数,并使用 setter:
Every class can have multiple constructors, each one can get different variables.
For this homework, you should add constructors that get the values similar to the setters functions, and use the setters:
这个问题只是意味着为有问题的类编写两个构造函数(您没有提到它的名称),它们执行与(c)和(d)部分中描述的方法相同的功能;即他们用小时和分钟,以及分别用小时、分钟和“am”标志来初始化类。
例如:
public void setTime(int hour, int 分钟)
public Time(int hour, int 分钟)
请注意,构造函数可以简单地链接到方法调用;例如
,但是,通常构造函数可用于初始化最终字段,因此不会链接到 setter;例如
The question simply means write two constructors for the class in question (you haven't mentioned the name of it) that perform the same function as the methods described in part (c) and (d); i.e. they initialise the class with the hour and minute, and with the hour, minute and "am" flag respectively.
For example:
public void setTime(int hour, int minute)
public Time(int hour, int minute)
Note that the constructor can simply chain to the method call; e.g.
However, typically a constructor may be used to initialise final fields and hence will not chain to a setter; e.g.