返回布尔值并在其他方法和对象中使用它

发布于 2024-12-22 15:24:54 字数 1506 浏览 3 评论 0原文

我要创建一个时钟,在两个不同的类中显示军事时间和正常时间,并将时间声明为对象。这已完成,但现在我需要将 clock 类扩展为 AMPMclock 中包含 setAMPM(boolean yes) 方法。如果setAMPM(true)它将显示军用时间,如果为false它将显示正常时间。我创建了 setAMPM 方法

class AMPMclock extends clock
{
    boolean setAMPM(boolean yes)
    { 
        return yes;
    }  
}

class clock
{
    private int hours;
    private int minutes;
    private int seconds;
private boolean yes;

    setMin....

    setHour...

    setSec....

    tick() ///moves the clock 1sec


    public String toString()
    {

        if(yes) return String.format("%d:%02d:%02d %s", (hours == 12 || hours == 0)?12 : hours%12, minutes, seconds, (hours < 12)? "AM" : "PM");
        else return String.format("%02d:%02d:%02d",hours,minutes,seconds);
    //else 
    }

}


MAIN

AMPMclock clockObject = new AMPMclock();

clockObject.setHour(16);
clockObject.setMin(28);
clockObject.setSec(58);



    System.out.println(clockObject.toString());
    clockObject.tick();
    clockObject.setAMPM(false);
    System.out.println(clockObject.toString());

    clockObject.tick();
    clockObject.setAMPM(true);
    System.out.println(clockObject.toString());
    clockObject.tick();
    System.out.println(clockObject.toString());

但是我使用 clockObject.setAMPM(true)clockObject.setAMPM(false) yes 布尔值不会改变,所以我可以在另一种方法中使用它。它总是保持不变...有什么帮助吗?总氮

I am to create a clock displaying Military and Normal time in two different classes and time declared as objects.That is done but now i need to extend the clock class into a
AMPMclock that has the setAMPM(boolean yes) method in it. If the setAMPM(true) it will display the military time and if it is false it will display the Normal time. I create the setAMPM method

class AMPMclock extends clock
{
    boolean setAMPM(boolean yes)
    { 
        return yes;
    }  
}

class clock
{
    private int hours;
    private int minutes;
    private int seconds;
private boolean yes;

    setMin....

    setHour...

    setSec....

    tick() ///moves the clock 1sec


    public String toString()
    {

        if(yes) return String.format("%d:%02d:%02d %s", (hours == 12 || hours == 0)?12 : hours%12, minutes, seconds, (hours < 12)? "AM" : "PM");
        else return String.format("%02d:%02d:%02d",hours,minutes,seconds);
    //else 
    }

}


MAIN

AMPMclock clockObject = new AMPMclock();

clockObject.setHour(16);
clockObject.setMin(28);
clockObject.setSec(58);



    System.out.println(clockObject.toString());
    clockObject.tick();
    clockObject.setAMPM(false);
    System.out.println(clockObject.toString());

    clockObject.tick();
    clockObject.setAMPM(true);
    System.out.println(clockObject.toString());
    clockObject.tick();
    System.out.println(clockObject.toString());

However whet i use clockObject.setAMPM(true) or clockObject.setAMPM(false)
the yes boolean will not change so i can use it in another method. It always stay the same...Any help? Tnx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

憧憬巴黎街头的黎明 2024-12-29 15:24:54

setter 的思想是封装一个私有实例变量。您只需返回参数而不修改类的内部状态,这就是代码中没有任何内容“粘连”的原因。

class Clock {

     private boolean useAMPM;

     public void setAMPM(boolean useAMPM) {
         this.useAMPM = useAMPM;
     }

     public boolean isAMPM() {
         return this.useAMPM;
     }
}

The idea of a setter is to encapsulate a private instance variable. You are simply returning the parameter back without modifying the internal state of the class, which is why nothing "sticks" in your code.

class Clock {

     private boolean useAMPM;

     public void setAMPM(boolean useAMPM) {
         this.useAMPM = useAMPM;
     }

     public boolean isAMPM() {
         return this.useAMPM;
     }
}
空‖城人不在 2024-12-29 15:24:54

如果您将设置保存在 ampm 实例变量中,那么您需要类似以下内容:

boolean ampm = true;

boolean setAMPM(boolean yes)
{
    ampm = yes;
    return yes;
}  

If you save a setting for example in an ampm instance variable, then you need something like:

boolean ampm = true;

boolean setAMPM(boolean yes)
{
    ampm = yes;
    return yes;
}  
人生百味 2024-12-29 15:24:54

也许您需要遵循 java bean 约定?

这是您想要实现的目标吗?

public class AMPMClock {

  private boolean isAMPM;

  public void setAMPM(boolean ampmFlag){
    this.isAMPM = ampmFlag;
  }
  public boolean isAMPM(){
   return this.isAMPM;
  }

}

May be you need to follow java bean conventions ?

Is this what you are aiming to achieve ?

public class AMPMClock {

  private boolean isAMPM;

  public void setAMPM(boolean ampmFlag){
    this.isAMPM = ampmFlag;
  }
  public boolean isAMPM(){
   return this.isAMPM;
  }

}
你与清晨阳光 2024-12-29 15:24:54

您可能需要将其存储在成员变量中...

    private boolean useAMPM = false;
    public boolean setAMPM (final boolean wantAMPM) {
          useAMPM = wantAMPM;
          return useAMPM;
    }
    public void draw () {
            … 
          if (useAMPM) { /* … */ } else { /* … */ }
    }

方法的 lambda 列表中的变量(定义方法时指定的变量,如 wantAMPM/yes)被分配当函数被调用时,并在函数返回时销毁。 (出于各种原因,将它们全部声明为 final 是一个好习惯。)事实上,您还可以在(几乎)任何一对 < Java 程序中的 code>{大括号} ,当到达 } 块末尾时,它同样会被销毁。

成员变量(属性)在它们所在的对象实例的生命周期内保留。因此,当您说 Clock ClockInstance = new Clock (); 时,clock.useAMPM 将被创建,并继续存在,直到 clockInstance被摧毁了。

you probably need to store that in a member variable…

    private boolean useAMPM = false;
    public boolean setAMPM (final boolean wantAMPM) {
          useAMPM = wantAMPM;
          return useAMPM;
    }
    public void draw () {
            … 
          if (useAMPM) { /* … */ } else { /* … */ }
    }

The variables in a method's lambda list (the ones you specify when you define a method, like wantAMPM/yes) are assigned when the function is called, and destroyed when the function returns. (It's a good habit to declare them all final, for various reasons, as well.) In fact, you can also create automatic variables within (almost) any pair of { braces } in a Java program, which will likewise be destroyed when you reach the end of the } block.

Member variables (properties) are kept around for the lifetime of the object instance in which they're found. So when, e.g. you say Clock clockInstance = new Clock ();, then clock.useAMPM will be created, and continue to exist until clockInstance is destroyed.

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