gpt4 book ai didi

Java Method.getName() 与字符串不匹配

转载 作者:行者123 更新时间:2023-12-01 07:36:18 29 4
gpt4 key购买 nike

我已经用几种不同的方法对此进行了测试。我比较的字符串s to 与日志文件中显示的完全相同。撇号是为了确保没有空格。有人知道这是怎么回事吗?

import java.lang.reflect.Method;
import android.util.Log;

public class Button {
public Button () {
for(Method m1:MyOtherClass.class.getMethods()) {
String s = m1.getName();
if(s == "Update") {
Log.i("result","true");
}
Log.i("test", "'" + s + "'");
}
}
}

最佳答案

您的问题在于:

if(s == "Update")

替换为

if (s.equals("Update"))

== 在处理对象(如字符串)时比较引用,而不是内容/值。

关于Java Method.getName() 与字符串不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11552578/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com