请检查以下程序并指出错误。无论输入什么,结果都是 "not ok"。
package timePass;
public class TimePass{
private String password;
public String getPassword() {
return password;
}
public void setPassword() {
password="no";
}
}
package timePass2;
import java.util.Scanner;
import timePass.TimePass;
public class TimePass2{
public static void main(String[] args) {
TimePass obj= new TimePass();
Scanner sc= new Scanner(System.in);
String p= sc.nextLine();
TimePass tp= new TimePass();
if(p.equals(tp.getPassword())) {System.out.println("ok");}
else {System.out.println("not ok");}
}
}
密码为 tp
没有被设置。你必须明确地调用 tp.setPassword()
否则 tp.password
是 null