为什么我的方法返回空密码?

发布于 2024-08-11 17:19:03 字数 1805 浏览 3 评论 0原文

我有两个类:生成器类和系统管理类。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

太阳哥哥 2024-08-18 17:19:04

我可以发现您的代码存在几个问题:

  • 您的 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 empty String as the password because you iterate over an empty StringBuilder with length 0.

  • When you create a new SystemManagement object with the parameter-less constructor, your password is null because you assign nothing to it (unless you use the setPassword setter).

  • When you create a new SystemManagement object with the constructor that takes a String, you ignore the parameter and your password is empty because nextPassword() always returns an empty String.

小鸟爱天空丶 2024-08-18 17:19:04

password1.length() 第一次是 0,所以它永远不会被填充?

password1.length() is 0 first time, so it never gets filled?

紫竹語嫣☆ 2024-08-18 17:19:04
 StringBuffer password1 = new StringBuffer();
    for (int i = 0; i < password1.length(); i++) {

我想字符串缓冲区的长度是 0

 StringBuffer password1 = new StringBuffer();
    for (int i = 0; i < password1.length(); i++) {

I would imagine that that would be 0 for the length of the string buffer.

痴者 2024-08-18 17:19:04

如果您调用不带参数的构造函数(例如 sys = new SystemManagement();),则永远不会设置新对象的 password 成员。仅当您调用采用 String 的构造函数时才会发生这种情况(您会忽略该构造函数)。

If you call the constructor with no arguments (like sys = new SystemManagement();), the new object's password member is never set. That only happens if you call the constructor that takes a String—which you ignore.

冰雪之触 2024-08-18 17:19:04
StringBuffer password1 = new StringBuffer();
for (int i = 0; i < password1.length(); i++) {
    password1.append(allowedCharacters[random.nextInt(allowedCharacters.length)]);
}

for 循环永远不会运行,因为此时 password1.length() 将为 0。

StringBuffer password1 = new StringBuffer();
for (int i = 0; i < password1.length(); i++) {
    password1.append(allowedCharacters[random.nextInt(allowedCharacters.length)]);
}

The for loop will never run as password1.length() will be 0 at this time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文