- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个公共(public)方法,它调用一个私有(private)方法,该方法有条件地调用另一个私有(private)方法。所有三个方法都返回一个 boolean 原始值,但是该值有时会与我期望的不同。
我确信我错过了一些非常明显的东西,但我现在太接近代码而无法看到问题。
这是代码的清理示例版本以及我看到的结果:
编辑:我已在底部添加实际代码
public class MyClass {
// Default Constructor
public MyClass() {}
public boolean foo(final Object arg0, final Object arg1, final Object arg2) {
final boolean result = this.bar(arg0, arg1, arg2);
System.out.println("foo: " + result);
return result;
}
private boolean bar(final Object arg0, final Object arg1, final Object arg2) {
boolean result = false;
System.out.println("bar: " + result);
try {
// complicated code that could generate an exception
if (this.wowsers(arg0, arg1, arg2)) {
result = true;
System.out.println("bar: " + result);
System.out.println("bar: call to wowsers() returned true");
}
} catch (Exception e) {
System.out.println("SOMETHING BLEW UP");
e.printStackTrace();
} finally {
// This should NOT change result
this.irrelevantMethod_1(null);
this.irrelevantMethod_2(null);
this.irrelevantMethod_3(null);
}
System.out.println("bar: " + result);
return result;
}
private boolean wowsers(final Object arg0, final Object arg1, final Object arg2) {
boolean result = false;
// complicated code involving the passed in arguments
// this MIGHT change result
System.out.println("wowsers: " + result);
return result;
}
private void irrelevantMethod_1(Object arg0) {
// Nothing in here to change result
}
private void irrelevantMethod_2(Object arg0) {
// Nothing in here to change result
}
private void irrelevantMethod_3(Object arg0) {
// Nothing in here to change result
}
} // END Class MyClass
调用代码:
MyClass myInstance = new MyClass();
myInstance.foo();
控制台输出:
> bar: false
> wowsers: true
> bar: true
> bar: call to wowsers() returned true
> foo: false
在上面的示例中,结果的值在方法 dowsers() 中设置为 true,并正确返回到 bar()。当 bar() 测试 wowsers() 返回的值并发现它为 true 时,它将自己的结果设置为 true 并将结果值打印到控制台,并打印 dowsers() 返回 true 。
bar() 末尾的 System.out.println 永远不会执行(至少我从未在控制台中看到任何内容),并且 result 的值(此时为 true)返回为 false。
然后 foo() 方法打印从调用 bar 中收到的值,该值始终为 false。
有人看出我哪里搞砸了吗?
编辑:这是实际代码 - 替换方法 foo() 和 bar()
public boolean sendReminderNotifcation(final RequestController controller, final Session session, final Request request,
final Comment comment, final boolean treatAsNewNote) {
final boolean result = this.sendNotification(controller, session, request, comment, null, MailType.Remind, treatAsNewNote);
System.out.println("sendReminderNotifcation(): " + result);
System.out.println("***");
return result;
}
private boolean sendNotification(final RequestController controller, final Session session, final Request request,
final Comment comment, final TreeSet<String> copyto, final MailType mailtype, final boolean treatAsNewNote) {
HashMap<NotifyWhom, TreeSet<String>> sendTo = null;
boolean result = false;
System.out.println("sendNotification(): " + result);
try {
if (null == controller) { throw new IllegalArgumentException("RequestContoller is null"); }
this.setRequestURLprefix(controller.getRequestURLprefix());
if (null == session) { throw new IllegalArgumentException("Session is null"); }
if (null == request) { throw new IllegalArgumentException("Request is null"); }
if (null == mailtype) { throw new IllegalArgumentException("mailtype is null"); }
final EnumSet<NotifyWhom> recipients = this.getRecipients(controller, session, request, mailtype, treatAsNewNote);
if ((null == recipients) || recipients.isEmpty()) { return false; }
final HashMap<NotifyWhom, TreeSet<String>> tempSendTo = this.getSendTo(controller, request, recipients);
if (null == tempSendTo) { throw new RuntimeException("NO RECIPIENTS FOR NOTIFICATION"); }
// clear out any duplicates
sendTo = this.purgeDuplicateRecpients(tempSendTo);
// Update Prior Assignee Information
// Update Requestor Information
// Update Queue Owner Information
this.updateReOpenedInformation(controller, session, request);
final String subject = (request.isReOpened()) ? HelpdeskNotifications.SUBJECT_REOPENED : HelpdeskNotifications.SUBJECT_UPDATED;
final Iterator<Entry<NotifyWhom, TreeSet<String>>> sit = sendTo.entrySet().iterator();
final TreeSet<NameHandle> exclude = this.getExcludeRecipients();
while (sit.hasNext()) {
final Map.Entry<NotifyWhom, TreeSet<String>> entry = sit.next();
final MailNotifyKey key = new MailNotifyKey(this.getLogger(), mailtype, entry.getKey());
if (MailType.Remind.equals(mailtype)) {
final Status status = request.getStatus();
final MailRecipientOption mro = (null == status) ? null : status.getMailRecipientOption(key);
// A null mro indicates that Notifications are DISABLED
if (null == mro) { return false; }
}
final TreeSet<String> sendto = entry.getValue();
if (this.sendEmail(controller, session, request, subject, comment, sendto, copyto, exclude, key, treatAsNewNote)) {
result = true;
System.out.println("sendNotification(): " + result);
System.out.println("sendNotification(): (call to sendEmail() returned true)");
}
}
// Send Special Re-Opened Notifications
if (this.sendReOpenedNotifications(controller, session, request, subject, comment, treatAsNewNote)) {
result = true;
System.out.println("sendNotification(): " + result);
System.out.println("sendNotification(): (call to sendReOpenedNotifications() returned true)");
}
} catch (final Exception e) {
this.getLogger().logException(this, e);
e.printStackTrace();
} finally {
this.setPriorAssigneeNotify(null);
this.setReOpenedRecipients(null);
this.setExcludeRecipients(null);
}
System.out.println("sendNotification(): " + result);
return result;
}
我得到的控制台输出是:
> sendNotification(): false
> sendNotification(): true
> sendNotification(): (call to sendEmail() returned true)
> sendReminderNotifcation(): false
> ***
(我意识到我应该首先发布实际代码)
出于某种原因,我无法弄清楚,方法 bar() 和方法 sendNotification() 中的最后两行代码似乎没有运行。是否还有其他我不知道的方法可以完成并返回?
最佳答案
我建议您在此行之前放置调试打印语句:
if (null == mro) { return false; }
因为这是一个 while 循环,并且允许该方法返回 false,即使 result
已设置为 true。我敢打赌这就是错误返回的来源,以及为什么您看不到正在执行的最终打印语句。
关于Java: boolean 原始私有(private)方法返回 false,即使设置为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26511892/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!