簡體   English   中英

即使在另一個類中實現,也無法訪問在接口中聲明的變量

[英]Can't access a variable declared in an interface even though it's implemented in another class

我有一個名為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. 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]);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM