有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法访问接口中声明的变量,即使它是在另一个类中实现的

我有一个名为StringInput的接口,其代码如下

 public static ArrayList<String[]> fileInput() throws IOException {
    ArrayList<String[]> lines = new ArrayList<>();
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line;
        while ((line = br.readLine()) != null) {
            String[] formatted = format(line);
            lines.add(formatted);
            System.out.println(line);
        }

    }
    return lines;
}

public static String[] format(String s) {
    String[] data = new String[9];
    String logEntryPattern = "^([\\d.]+) (\\S+) (\\S+) \\[([\\w:/]+\\s[+\\-]\\d{4})\\] \"(.+?)\" (\\d{3}) (\\d+) \"([^\"]+)\" \"([^\"]+)\"";

    System.out.println("Using RE Pattern:");
    System.out.println(logEntryPattern);
    System.out.println("Combined Apache Log Format (VERBOSE)");

    System.out.println("Input line is:");
    System.out.println(s);

    Pattern p = Pattern.compile(logEntryPattern);
    Matcher matcher = p.matcher(s);
    if (!matcher.matches()
            || NUM_FIELDS != matcher.groupCount()) {
        System.err.println("Bad log entry (or problem with RE?):");
        System.err.println(s);
        return null;
    }

    System.out.println("IP Address: " + matcher.group(1));
    String clientIP = matcher.group(1);
    System.out.println("Identd: " + matcher.group(2));
    String identd = matcher.group(2);
    System.out.println("UserID: " + matcher.group(3));
    String userID = matcher.group(3);
    System.out.println("Date&Time: " + matcher.group(4));
    String dateTime = matcher.group(4);
    System.out.println("Request: " + matcher.group(5));
    String protocol = matcher.group(5);
    System.out.println("Response: " + matcher.group(6));
    String respCode = matcher.group(6);
    System.out.println("Bytes Sent: " + matcher.group(7));
    String respSize = matcher.group(7);
    System.out.println("Referer: " + matcher.group(8));
    String refer = matcher.group(8);
    System.out.println("Browser: " + matcher.group(9));
    String userAgent = matcher.group(9);
    data[0] = clientIP;
    data[1] = identd;
    data[2] = userID;
    data[3] = dateTime;
    data[4] = protocol;
    data[5] = respCode;
    data[6] = respSize;
    data[7] = refer;
    data[8] = userAgent;
    return data;
}

和另一个名为ParseUpload的类,该类实现该接口并包含一个main方法。在main方法中,我有一个准备好的语句,试图从接口调用变量。但是,我不能。我不知道,因为我通常不使用接口,不实例化它对我来说很奇怪。 有什么帮助吗?谢谢

编辑:这是另一个类

public class ParseUpload implements StringInput {

public static void main(String argv[]) {

    try {
        StringInput.fileInput();

    } catch (Exception e) {
        e.printStackTrace();
    }

    //while (logEntryLine !=null){
    // for (int i = 0; i < file.length(); i++){
    //System.out.println(fileExists); ignore/For testing purposes
    Connection conn = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:5294/mysql", "root", "Iu&#KR489)(");
        System.out.println("Database Connection Established!");

        String query = " insert into alogs(clientIP, identd, userID, dateTime, protocol, respCode, respSize, refer, userAgent)"
                + " values (?, ?, ?, ? ,? ,? ,?, ?, ?)";

        PreparedStatement preparedStmt = con.prepareStatement(query);
        preparedStmt.setString(1, data);
        preparedStmt.setString(2, identd);
        preparedStmt.setString(3, userID);
        preparedStmt.setString(4, dateTime);
        preparedStmt.setString(5, protocol);
        preparedStmt.setString(6, respCode);
        preparedStmt.setString(7, respSize);
        preparedStmt.setString(8, refer);
        preparedStmt.setString(9, userAgent);
        preparedStmt.execute();

        con.close();

    } catch (SQLException ex) {
        System.out.println("****************************Can't Connect to the database***********************************");
        System.out.println("SQLException: " + ex.getMessage());
        System.out.println("SQLState: " + ex.getSQLState());
        System.out.println("VendorError: " + ex.getErrorCode());
    } catch (ClassNotFoundException e) {
        System.out.println("Class Not Found");

    }
    // }
}

}


共 (1) 个答案

  1. # 1 楼答案

    您的问题不是接口问题
    在您的情况下,您应该:

    1. StringInput是一个带有一些静态方法的类(这些方法将返回一个变量,您可以使用它)
    2. ParseUpload调用这些方法以获得结果并使用它

    下面是一个简短的例子:

    public class StringInput {
        public static String[] format() {
            String[] datas = new String[2];
            datas[0] = "Some";
            datas[1] = "thing";
            return datas;
        }
    }
    

    以及:

    public class ParseUpload {
        public static void main(String[] args) {
            String[] resultOfFormatCall = StringInput.format();
            // Now you have the result of your format method, and you are able to use it
            System.out.println("result[0]" + resultOfFormatCall[0] + ";result[1]" + resultOfFormatCall[1]);
        }
    }