返回布尔值并在其他方法和对象中使用它
我要创建一个时钟,在两个不同的类中显示军事时间和正常时间,并将时间声明为对象。这已完成,但现在我需要将 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 aAMPMclock
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
setter 的思想是封装一个私有实例变量。您只需返回参数而不修改类的内部状态,这就是代码中没有任何内容“粘连”的原因。
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.
如果您将设置保存在 ampm 实例变量中,那么您需要类似以下内容:
If you save a setting for example in an ampm instance variable, then you need something like:
也许您需要遵循 java bean 约定?
这是您想要实现的目标吗?
May be you need to follow java bean conventions ?
Is this what you are aiming to achieve ?
您可能需要将其存储在成员变量中...
方法的 lambda 列表中的变量(定义方法时指定的变量,如
wantAMPM
/yes
)被分配当函数被调用时,并在函数返回时销毁。 (出于各种原因,将它们全部声明为final
是一个好习惯。)事实上,您还可以在(几乎)任何一对 < Java 程序中的 code>{大括号} ,当到达}
块末尾时,它同样会被销毁。成员变量(属性)在它们所在的对象实例的生命周期内保留。因此,当您说
Clock ClockInstance = new Clock ();
时,clock.useAMPM
将被创建,并继续存在,直到clockInstance
被摧毁了。you probably need to store that in a member variable…
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 allfinal
, 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 ();
, thenclock.useAMPM
will be created, and continue to exist untilclockInstance
is destroyed.