简体   繁体   English

如何从Java中的私有机构访问变量

[英]how to access variables from private bodies in java

public void TheBank() {
Scanner Sc = new Scanner(System.in);
    System.out.println("Please Enter your Username and Password");
    MemoryStorage();// Stores the Username and Password in the system.
    VariableCompare();// Compares the Username and Password to see if they match or do not
}

private void MemoryStorage(){// this should just be here to store the variables username and password
    Scanner Sc = new Scanner(System.in);
    String username = Sc.nextLine();
    String password = Sc.nextLine();
}
private void VariableCompare() {// this should compare to see if they match or dont and print the results
    if (username.equals (password){
        System.out.println("Please try again your Username and Password cannot be the same");
    } else {
        System.out.println (" Your username is:" + username);
        System.out.println(" Your password is:" + password);
    }
}
}

My question is the body (MemoryStorage), i want it to save the username and password, so that the other bodies can use it aswell to proceed with their calculations. 我的问题是正文(MemoryStorage),我希望它保存用户名和密码,以便其他正文也可以使用它来进行计算。 At the moment the variables username and password are not accepted into the other bodies and i want to know how i can make those two variables available to all bodies to use. 目前,变量用户名和密码尚未被其他机构接受,我想知道如何使这两个变量可供所有机构使用。

Currently you're declaring local variables. 当前,您正在声明局部变量。 Those only exist while you're executing the method. 这些仅在执行方法时存在。 It sounds like they should actually be instance variables (fields) within your class: 听起来它们实际上应该是您的类中的实例变量(字段):

public class Whatever {
    private String username;
    private String password;

    // Methods which assign to username/password and read from them
}

You should also read up on naming conventions in Java, and think about how to name your methods more along the lines of what they're actually doing. 您还应该阅读Java中的命名约定,并考虑如何按照方法的实际名称来命名它们。 Oh, and consider passing the Scanner into your current MemoryStorage method rather than creating a new one - I've seen various problems when people create multiple instances of Scanner with the same underlying stream. 哦,考虑将Scanner传递到当前的MemoryStorage方法中,而不是创建一个新的方法-当人们使用相同的基础流创建多个Scanner实例时,我已经看到了各种问题。

As an alternative to instance fields, you could consider using return values from the methods. 作为实例字段的替代方法,您可以考虑使用方法的返回值。 For example: 例如:

AuthenticationInfo auth = requestUserAuthentication(scanner);
validateAuthentication(auth);

... again though, it's not clear that these should really be separate methods. ……不过,还不清楚这些方法是否真的应该是单独的方法。 Why is the method which asks for the auth info not validating it? 为什么要求身份验证信息的方法无法对其进行验证? Note that the validation you're using is a simple one which isn't actually checking that the username/password combination is correct - it's just checking that it's possibly correct (the same sort of validation as would check for a minimal password length etc). 请注意,您使用的验证是一个简单的一个实际不检查用户名/密码组合是正确的 -它只是检查它的可能是正确的(相同类型的验证作为将检查最小密码长度等) 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM