当我运行此代码时,输出是"字符串"; 如果我隐藏接受String参数的方法并再次运行代码然后输出是"对象",那么有人可以解释一下这段代码是如何工作的吗?
public class Example {
static void method(Object obj) {
System.out.println("Object");
}
static void method(String str) {
System.out.println("String");
}
public static void main(String args[]) {
method(null);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读Drools 6.4.0 文档。我不确定是否理解contains和memberof运算符之间的区别:两者都适用于集合,似乎唯一的区别是contains期望包含的对象在右侧,而memberof期望它在左侧,所以它们是等效的,但我恐怕我错过了一些东西。
在我们的项目中,我们使用 Spring Boot 2.1.3.Release,对于调度程序作业,我们在方法级别使用了 @Scheduled。
@Scheduled(fixedRate = 1000)
public void fixedRateSchedule() {
System.out.println(
"Fixed rate task - " + System.currentTimeMillis() / 1000);
}
Run Code Online (Sandbox Code Playgroud)
固定速率不等待前一个任务完成。
@Scheduled(fixedDelay = 1000)
public void fixedDelaySchedule() {
System.out.println(
"Fixed delay task - " + System.currentTimeMillis() / 1000);
}
Run Code Online (Sandbox Code Playgroud)
fixedDelay 任务总是等到前一个任务完成。
@Scheduled(cron = "0 0/5 * * * ?")
public void fixedDelaySchedule() {
System.out.println(
"cron task - " + System.currentTimeMillis() / 1000);
}
Run Code Online (Sandbox Code Playgroud)
上面的 cron 将每五分钟执行一次,我的问题是:@scheduled cron 是否会在触发下一个作业之前等待上一个任务完成?
在Internet上我发现这段代码生成SHA1哈希:
public static String hash(String str) {
try {
MessageDigest mg = MessageDigest.getInstance("SHA-1");
byte[] result = mg.digest(str.getBytes());
StringBuffer sb = new StringBuffer();
for (int i = 0; i < result.length; i++) {
sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
}
return sb.toString();
} catch (NoSuchAlgorithmException e) {
System.err.println("SHA-1 not found.");
return "";
}
}
Run Code Online (Sandbox Code Playgroud)
但为什么会这样(result[i] & 0xff) + 0x100?
我更新AppCompat到新发布的版本22.1.0并将我更改AlertDialog为support.v7.app.AlertDialog.但是在Lollipop设备上,它会抛出以下异常dismissDialog().
java.lang.NullPointerException: attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v7.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:248)
at android.support.v7.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:201)
at android.support.v7.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:184)
at android.support.v7.app.AppCompatDeleg ateImplV7.cre ateSupportActionBar(AppCompatDelegateImplV7.java:176)
at android.support.v7.app.AppCompatDelegateImplBase.getSupportActionBar(AppCompatDelegateImplBase.java:85)
at android.support.v7.app.AppCompatDelegateImplV7.onStop(AppCompatDeleg ateImplV7.java:221)
at android.support.v7.app.AppCompatDialog.onStop(AppCompatDialog.java:108)
at android.app.Dialog.dismissDialog(Dialog.java:438)
at android.app.Dialog.dismiss(Dialog.java:414)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:157)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5834)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
Run Code Online (Sandbox Code Playgroud)
我如何解决它?
(较低版本的设备似乎运行良好.这只发生在Lollipop)
+
我没有dismiss()在我的代码中明确调用.当按钮被后退按钮或正/负按钮关闭时,该对话框将抛出异常.
++这是我使用v7.app.AlertDialog的代码.谢谢.
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment; …Run Code Online (Sandbox Code Playgroud) public void getData(int i){
System.out.println("1");
}
public void getData(Integer i){
System.out.println("2");
}
Run Code Online (Sandbox Code Playgroud)
以下代码行
this.getClass().getMethod("getData",Integer.class).invoke(this, 10);
Run Code Online (Sandbox Code Playgroud)
打印2,如何打印1?
java ×4
android ×1
cron ×1
drools ×1
hex ×1
integer ×1
reflection ×1
sha1 ×1
spring-boot ×1
string ×1