为什么我的方法返回空密码?
我有两个类:生成器类和系统管理类。 Generator 类,我可以生成密码。 SystemManagement 类导入 Generator(从另一个包)并包含附加信息。
当我创建一个 SystemManagement 对象并对其调用 getPassword()
时,我返回 null
。为什么会发生这种情况?
生成器:
public class Generator {
private static String password;
private static Generator instance;
public String nextPassword() {
char[] allowedCharacters = {'a', 'b', 'c', 'd', 'e', 'f', '1', '2', '3', '4', '5','6'};
SecureRandom random = new SecureRandom();
StringBuffer password1 = new StringBuffer();
for (int i = 0; i < password1.length(); i++) {
password1.append(allowedCharacters[random.nextInt(allowedCharacters.length)]);
}
password = password1.toString();
return password;
}
public String currentPassword() {
return password;
}
public static void setPassword(String password) {
Generator.password = password;
}
public static Generator getInstance() {
if (instance == null) {
instance = new Generator();
}
return instance;
}
}
系统管理:
public class SystemManagement implements Serializable {
private String name;
private String family;
private String password;
public SystemManagement() {
}
public SystemManagement(String password) {
this.password = Generator.getInstance().nextPassword();
}
public Students addStudent(String name, String family) {
Students student = new Students(name, family);
students.add(student);
return student;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
I have two classes: a Generator class and a SystemManagement class. The Generator class, I can generate a password. The SystemManagement class imports Generator (from another package) and includes additional information.
When I make a SystemManagement object and call getPassword()
on it, I get back null
. Why is this happening?
Generator:
public class Generator {
private static String password;
private static Generator instance;
public String nextPassword() {
char[] allowedCharacters = {'a', 'b', 'c', 'd', 'e', 'f', '1', '2', '3', '4', '5','6'};
SecureRandom random = new SecureRandom();
StringBuffer password1 = new StringBuffer();
for (int i = 0; i < password1.length(); i++) {
password1.append(allowedCharacters[random.nextInt(allowedCharacters.length)]);
}
password = password1.toString();
return password;
}
public String currentPassword() {
return password;
}
public static void setPassword(String password) {
Generator.password = password;
}
public static Generator getInstance() {
if (instance == null) {
instance = new Generator();
}
return instance;
}
}
SystemManagement:
public class SystemManagement implements Serializable {
private String name;
private String family;
private String password;
public SystemManagement() {
}
public SystemManagement(String password) {
this.password = Generator.getInstance().nextPassword();
}
public Students addStudent(String name, String family) {
Students student = new Students(name, family);
students.add(student);
return student;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可以发现您的代码存在几个问题:
您的
nextPassword()
方法始终返回一个空的String
作为密码,因为您迭代了一个空的StringBuilder
长度为 0。当您使用无参数构造函数创建新的
SystemManagement
对象时,您的密码
为 null,因为您没有为其分配任何内容 (除非您使用setPassword
setter)。当您使用采用
String
的构造函数创建新的SystemManagement
对象时,您会忽略该参数,并且您的password
为空,因为 < code>nextPassword() 始终返回空String
。I can spot a couple of problems with your code:
Your
nextPassword()
method always returns an emptyString
as the password because you iterate over an emptyStringBuilder
with length 0.When you create a new
SystemManagement
object with the parameter-less constructor, yourpassword
is null because you assign nothing to it (unless you use thesetPassword
setter).When you create a new
SystemManagement
object with the constructor that takes aString
, you ignore the parameter and yourpassword
is empty becausenextPassword()
always returns an emptyString
.password1.length() 第一次是 0,所以它永远不会被填充?
password1.length() is 0 first time, so it never gets filled?
我想字符串缓冲区的长度是
0
。I would imagine that that would be
0
for thelength
of the string buffer.如果您调用不带参数的构造函数(例如
sys = new SystemManagement();
),则永远不会设置新对象的password
成员。仅当您调用采用String
的构造函数时才会发生这种情况(您会忽略该构造函数)。If you call the constructor with no arguments (like
sys = new SystemManagement();
), the new object'spassword
member is never set. That only happens if you call the constructor that takes aString
—which you ignore.for 循环永远不会运行,因为此时
password1.length()
将为 0。The for loop will never run as
password1.length()
will be 0 at this time.