【发布时间】:2016-09-03 15:15:33
【问题描述】:
我对 Java 很陌生,我尝试做的事情可能看起来很奇怪,但我需要对 Java 的工作原理有所了解,而不是实际完成一个设定的结果。
如果我有一个布尔方法,例如:
public class doThings
{
// imagine the intial value of this variable is set
// and or modified using getters and setters between the declaration
// and the following method
private boolean boolVar;
public boolean doSomething()
{
if(boolVar == true)
{
//does things and then returns true
return true;
}
else
{
return false;
}
}
}
我想在另一个类中调用这个方法......
public class testDoThings
{
doThings someObject = new doThings;
public static void main(String[] args)
{
someObject.doSomething()
}
}
我如何检查(在 testDoThings 类中)以查看该方法是返回 true 还是返回 false 并相应地打印类似这样的消息...
public class testDoThings
{
doThings someObject = new doThings;
public static void main(String[] args)
{
someObject.doSomething()
if (doSomething() returns true) // yes I am aware this is not
//valid
{
// print a statment
}
else if (doSomething() returns false) // yes once again not valid
{
// print a different statement
}
}
}
我知道您可以将这些消息放在包含该方法的类中,但是如果我想要不同的消息,具体取决于调用方法的位置和调用的对象,那么将内容放在原始类方法中并不总是可行上班。
如果我在这里完全偏离轨道,请告诉我,如果有人可以向我解释这一点,那就太好了!
【问题讨论】: