关于java:我怎样才能摆脱“静态”?

How can I get rid of “static”?

本问题已经有最佳答案,请猛点这里访问。

这是我的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class Main extends JFrame{

static int NoOfUsers;
static String[][] Accounts = new String[NoOfUsers][2];

public static void main(String[] args){
    Login();
}

private static void Login() {
    final String FileName ="F:/TextFiles/loginaccs.txt";
    try {
        BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(FileName)));
        int NoOfUsersL = Integer.parseInt(file.readLine());
        String[][] Accounts = new String[NoOfUsersL][2];
        NoOfUsers = NoOfUsersL;
        for (int i=0; i<NoOfUsersL; i++) {
            Accounts[i][0] = file.readLine();
            Accounts[i][1] = file.readLine();
        }
        for (int i=0; i<NoOfUsersL; i++) {
            System.out.println(Accounts[i][0]);
            System.out.println(Accounts[i][1]);
        }
        file.close();

    } catch (IOException e) {
        System.out.println("ERROR: unable to read file.");
        e.printStackTrace();  
    }    

    String username = null;
    String password = null;
    JTextField usernamejtf = new JTextField(username);
    JPasswordField passwordjtf = new JPasswordField(password);
    String[] buttons = {"Login","Create new account"};
    Object[] InputDialog = {
           "Username:", usernamejtf,"Password:", passwordjtf
    };

    do {

    int option = JOptionPane.showOptionDialog(null,
            InputDialog,
           "Login",
            JOptionPane.DEFAULT_OPTION,
            JOptionPane.PLAIN_MESSAGE,
            null,
            buttons,
            buttons[0]);
    System.out.println(option); //Check

    if (option == JOptionPane.CLOSED_OPTION ) {
        return;
    }
    else if (option == 0) {
        if (CheckAccount(username,password)) {
            System.out.println("Logged in");
        } else {
            System.out.println("Wrong Password/Username");
        }
    } else if (option == 1) {
        System.out.println("Create Account.");
        }

    } while (!(CheckAccount(username,password)));
}


private static boolean CheckAccount(String username, String password) {
    for (int i=0; i>NoOfUsers; i++) {
        if ((username == Accounts[i][0]) && (password == Accounts[i][1])) {
            return true;
        }
    }
    return false;
}

}

在"main"中,我调用了login()方法,Eclipse强迫我将单词"static"放在方法名前面。我是否可以修改程序,使行可以写为:private void login()…私有布尔支票帐户(…)…等?

[附加问题:因为"static"这个词,我不能把"public"这个词放在string[]accounts=new string[noolfusersl][2]之前;这使得checkaccount无法访问accounts数组。如何修改程序以解决此问题。]提前通知大家。


静态方法静态方法是呼叫(不知道安安审审法)的呼叫建立和呼叫从第一页第一类为as belowP></

1
2
3
4
5
6
public static void main(String[] args){
    Main main = new Main();    
    main.login();
}

private void login() { //remove static from instance methods


在静态qualifiers阶to remove方法,你必须让你的布尔变量的方法和实例变量。然后,你可以和你的呼叫login()JFrame对象实例化。P></

转换的方法也mixedcase /变量名称As to Java offical公约。恩,你把我# VB或C语言编程?P></

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
    private static final long serialVersionUID = -105943237549003486L;

    private int numOfUsers;
    private String[][] accounts;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Main m = new Main();

                m.login("F:/TextFiles/loginaccs.txt");
            }
        });
    }

    // Constructor, set up instance values here.
    public Main() {
        super();
    }

    private void login(final String fileName) {
        try {
            BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

            numOfUsers = Integer.parseInt(file.readLine());
            accounts = new String[numOfUsers][2];

            for (int i = 0; i < numOfUsers; i++) {
                accounts[i][0] = file.readLine();
                accounts[i][1] = file.readLine();
            }

            for (int i = 0; i < numOfUsers; i++) {
                System.out.println(accounts[i][0]);
                System.out.println(accounts[i][1]);
            }

            file.close();

        } catch (IOException e) {
            System.out.println("ERROR: unable to read file.");
            e.printStackTrace();
        }

        String username = null;
        String password = null;
        JTextField usernamejtf = new JTextField(username);
        JPasswordField passwordjtf = new JPasswordField(password);
        String[] buttons = {"Login","Create new account" };
        Object[] InputDialog = {"Username:", usernamejtf,"Password:", passwordjtf };

        do {
            int option = JOptionPane.showOptionDialog(null, InputDialog,"Login", JOptionPane.DEFAULT_OPTION,
                    JOptionPane.PLAIN_MESSAGE, null, buttons, buttons[0]);
            System.out.println(option); // Check

            if (option == JOptionPane.CLOSED_OPTION) {
                return;
            } else if (option == 0) {
                if (checkAccount(username, password)) {
                    System.out.println("Logged in");
                } else {
                    System.out.println("Wrong Password/Username");
                }
            } else if (option == 1) {
                System.out.println("Create Account.");
            }

        } while (!(checkAccount(username, password)));
    }

    private boolean checkAccount(String username, String password) {
        for (int i = 0; i > numOfUsers; i++) {
            if (username == accounts[i][0] && password == accounts[i][1]) {
                return true;
            }
        }

        return false;
    }
}


使用:P></

1
2
3
4
public static void main(String[] args){
    Main main = new Main();
    main.login();
}


new Main().login();P></

You should naming约定后续变量和Java method should start,lowercase名称与字母。P></