添加与其他构造函数类似的构造函数意味着什么?

发布于 2024-12-11 08:54:20 字数 959 浏览 0 评论 0原文

添加两个类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天旳寂寞 2024-12-18 08:54:21

每个类可以有多个构造函数,每个构造函数可以获取不同的变量。

对于本作业,您应该添加获取类似于 setter 函数的值的构造函数,并使用 setter:

    public ClassName(int newHour, int newMinute)
    {
        SeTime(newHour, newMinute);
    }

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:

    public ClassName(int newHour, int newMinute)
    {
        SeTime(newHour, newMinute);
    }
我也只是我 2024-12-18 08:54:21

这个问题只是意味着为有问题的类编写两个构造函数(您没有提到它的名称),它们执行与(c)和(d)部分中描述的方法相同的功能;即他们用小时和分钟,以及分别用小时、分钟和“am”标志来初始化类。

例如:

  • 方法:public void setTime(int hour, int 分钟)
  • 类似的构造函数:public Time(int hour, int 分钟)

请注意,构造函数可以简单地链接到方法调用;例如

public Time(int hour, int minute) {
  setTime(hour, minute);
}

,但是,通常构造函数可用于初始化最终字段,因此不会链接到 setter;例如

public Time(int hour, int minute) {
  this.hour = hour;
  this.minute = minute;
}

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:

  • Method: public void setTime(int hour, int minute)
  • Analagous constructor: public Time(int hour, int minute)

Note that the constructor can simply chain to the method call; e.g.

public Time(int hour, int minute) {
  setTime(hour, minute);
}

However, typically a constructor may be used to initialise final fields and hence will not chain to a setter; e.g.

public Time(int hour, int minute) {
  this.hour = hour;
  this.minute = minute;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文