Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取已创建的MatchAllFilters和其他筛选器的名称?需要对接口进行哪些更改?_Java_Arraylist_Filter_Interface_Filtering - Fatal编程技术网

Java 如何获取已创建的MatchAllFilters和其他筛选器的名称?需要对接口进行哪些更改?

Java 如何获取已创建的MatchAllFilters和其他筛选器的名称?需要对接口进行哪些更改?,java,arraylist,filter,interface,filtering,Java,Arraylist,Filter,Interface,Filtering,我需要对“所有过滤器类”进行哪些更改?对于MatchAllFilter类,getName方法应该返回其ArrayList中所有过滤器名称的字符串,我不知道如何执行该操作。我还想知道如何在类似过滤器的深度中使用getName方法 public interface Filter { public String getName(); public boolean satisfies(QuakeEntry qe); } public class DepthFilter impleme

我需要对“所有过滤器类”进行哪些更改?对于MatchAllFilter类,getName方法应该返回其ArrayList中所有过滤器名称的字符串,我不知道如何执行该操作。我还想知道如何在类似过滤器的深度中使用getName方法

public interface Filter { 
   public String getName();
   public  boolean satisfies(QuakeEntry qe);
}


public class DepthFilter implements Filter {
    private double depthMin;
    private double depthMax;
    private String fName;

    public DepthFilter(double min, double max, String name) {
        depthMin = min;
        depthMax = max;
        fName = name;

    }

    public boolean satisfies(QuakeEntry qe) {
        if (qe.getDepth() >= depthMin && qe.getDepth() <= depthMax) {
            return true;
        }

        return false;
    }
}

public class MatchAllFilter implements Filter {
    private ArrayList<Filter> filters;
    private String fName;

    public MatchAllFilter() {
        filters = new ArrayList<Filter>();

    }

public void addFilter(Filter f){
    filters.add(f);

    public boolean satisfies(QuakeEntry qe) {
        for (Filter f : filters) {
            if (!f.satisfies(qe)) { // any of the filters criteria failed, then exit
                return false;
            }
        }
        return true;
    }
}
公共接口筛选器{
公共字符串getName();
公共布尔满足(QuakeEntry qe);
}
公共类DepthFilter实现过滤器{
私人双深度;
私人双深度最大;
私有字符串fName;
公共深度筛选器(双最小值、双最大值、字符串名称){
深度min=min;
深度最大=最大值;
fName=名称;
}
公共布尔满足(QuakeEntry qe){

如果(qe.getDepth()>=depthMin&&qe.getDepth()您的每个过滤器
都实现了过滤器
对吗

接口
Filter
有一个
getName()
,您必须在每个类上实现它

也许每个过滤器在构造函数中都需要一种方法来传递
字符串名
,对吗

然后,使用
getName()

例如:

DepthFilter depthFilter = new DepthFilter(1.5, 5.5, "myOwnFilter");
String name = depthFilter.getName();

关于Java中的接口:

List<String> list = new ArrayList<>();
list.add("one"); list.add("two"); list.add("three"); // Make a list of 3 elements
String allStrings = ""; // This will have all the elements of the collection
// Iterate the list collection and get each element and concatenate
for (String ele : list) {
    allStrings = allStrings + ele;
}
System.out.println(allStrings); // This prints: onetwothree
接口是关于定义一个类可以做什么的约定,而不涉及该类将如何做。该约定是通过在类中实现接口方法来实现的

  • 接口具有抽象方法和常量
  • 类实现接口-使用关键字implements
  • 一个类可以实现多个接口
来自帖子的代码示例:

List<String> list = new ArrayList<>();
list.add("one"); list.add("two"); list.add("three"); // Make a list of 3 elements
String allStrings = ""; // This will have all the elements of the collection
// Iterate the list collection and get each element and concatenate
for (String ele : list) {
    allStrings = allStrings + ele;
}
System.out.println(allStrings); // This prints: onetwothree
接口
Filter
有两个抽象方法:
getName
满足
。实现类必须实现这两个抽象方法(并且可以定义其他方法)。实现抽象方法涉及提供方法体;例如:

public String getName() {
    return fName;
}
使用接口也被认为是一种继承类型;通常,Java中的继承是关于扩展类的。上面的注释涉及Java 8之前的版本,Java SE 8中的接口可以有默认方法和静态方法(可选)

字符串和连接:

List<String> list = new ArrayList<>();
list.add("one"); list.add("two"); list.add("three"); // Make a list of 3 elements
String allStrings = ""; // This will have all the elements of the collection
// Iterate the list collection and get each element and concatenate
for (String ele : list) {
    allStrings = allStrings + ele;
}
System.out.println(allStrings); // This prints: onetwothree
对于MatchAllFilter类,getName方法应该返回 它的ArrayList中所有过滤器名称的字符串,我不知道如何使用 去做

如上所述,必须实现
getName
方法。如何在
ArrayList
中生成所有元素的字符串?可以获取每个名称并将其连接在一起。显然,这涉及到
string
连接-下面是一些示例代码:

String s1 = "one";
String s2 = "two";
String concatenatedString = s1 + " * " + s2;
System.out.println(concatenatedString); // This prints: one * two
请注意,还可以使用
concat
方法连接字符串

连接ArrayList集合中的所有元素:

List<String> list = new ArrayList<>();
list.add("one"); list.add("two"); list.add("three"); // Make a list of 3 elements
String allStrings = ""; // This will have all the elements of the collection
// Iterate the list collection and get each element and concatenate
for (String ele : list) {
    allStrings = allStrings + ele;
}
System.out.println(allStrings); // This prints: onetwothree

@shmosel我正在努力学习如何从书本和MOOC中编写代码。实际上,除了StackOverflow之外,我没有其他人可以寻求帮助。我的代码已经按原样工作了,但getName()部分正在放弃一切。我是否将构造函数放入
公共字符串getName(){return fName;}
我还需要一个私有变量吗?看看DepthFilter的构造函数如何将
名称
参数保存到
fname
中?您可能需要对
匹配所有筛选器
执行相同的操作,因为我将
公共字符串getName()
添加到需要添加
公共字符串getName()的接口中
到每个筛选器,我需要在那里返回名称。这是我特别努力解决的问题。无论您在哪里使用接口。
公共类DepthFilter实现筛选器{private double depthMin;private double depthMax;private String fName;public DepthFilter(double min,double max,String name){depthMin=min;depthMax=max;fName=name;}
我在这里具体添加什么。