Java作业求助:实现Filter与QueryResolver类统计字符匹配次数
Hey there! Let's tackle your assignment step by step—since you're new to Java and a non-native English speaker, I'll keep things clear and straightforward.
Let's Break Down Your Problems First
1. Non-static contains Method Error
The error "Non-static variable cannot be referenced from a static context" happens because static methods belong to the class itself, while non-static methods belong to individual objects (instances) of the class. To call contains(), you need to first create an instance of the Filter class, then use that instance to call the method.
2. Implementing toString()
The toString() method is used to return a human-readable string representation of your object. For the Filter class, it should describe what the filter contains—usually, we return the stored filter string itself, or a descriptive message.
3. Building the Chainable QueryResolver
Chainable methods work by returning the instance of the class (this) after setting values. So where() and matches() will return the QueryResolver instance, allowing you to chain calls like resolver.where(query).matches(filter).count().
Complete Working Code Example
1. The Filter Class
public class Filter { // Member variable to store the filter string (must be non-static) private String letters; // Constructor: Initialize the filter with the given letters public Filter(String letters) { this.letters = letters; } // Non-static method: Check if a character exists in the filter public boolean contains(char character) { // Iterate through each character in the filter string for (char c : letters.toCharArray()) { if (c == character) { return true; } } return false; } // toString() implementation: Return a readable representation of the Filter @Override public String toString() { // Option 1: Return just the filter string // return letters; // Option 2: Return a descriptive message (more informative) return "Filter{letters='" + letters + "'}"; } }
2. The QueryResolver Class (Chainable)
public class QueryResolver { // Private member variables to store query and filter instances private String query; private Filter filter; // Chainable method: Set the query string and return this instance public QueryResolver where(String query) { this.query = query; return this; } // Chainable method: Set the Filter instance and return this instance public QueryResolver matches(Filter filter) { this.filter = filter; return this; } // Count method: Use Filter's contains() to count matches (non-static!) public int count() { int matchCount = 0; // Iterate through each character in the query for (char c : query.toCharArray()) { // Use the Filter instance to check if the character exists if (filter.contains(c)) { matchCount++; } } return matchCount; } }
3. Test the Code with a Main Method
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter the query string:"); String query = in.next(); System.out.println("Please enter the filter string:"); String filterStr = in.next(); // Create Filter instance Filter filter = new Filter(filterStr); // Use chainable QueryResolver QueryResolver resolver = new QueryResolver(); int count = resolver.where(query).matches(filter).count(); System.out.println("\nThe filter string '" + filterStr + "' has characters that appear in '" + query + "' exactly " + count + " times!"); // Test the toString() method System.out.println("Filter toString(): " + filter); in.close(); } }
Key Explanations
Filter Class:
- We added a private member variable
lettersto store the filter string (this is missing in your initial code). - The
contains()method uses the instance'slettersvariable to check for the character—no static context issues here because we call it on a Filter instance. toString()is overridden to return a clear description of the Filter object.
- We added a private member variable
QueryResolver Class:
where()andmatches()returnthis(the current QueryResolver instance) to enable chaining.count()is non-static, and it uses the Filter instance'scontains()method to count matches—exactly what your assignment requires.
Testing:
- We create a Filter instance first, then pass it to the QueryResolver's
matches()method. This avoids the static context error because we're working with object instances, not static class methods.
- We create a Filter instance first, then pass it to the QueryResolver's
内容的提问来源于stack exchange,提问作者StudentAccount4

