有些奇怪:我无法使用在导入的类中实现的方法。 我有一个名为“Person.java”的类,其中包含一个名为“isFrom()”的实现方法,如下所示:
import java.util.List;
import java.util.function.Function;
public class Person implements Comparable<Person> {
private String name;
private String role;
private String city;
private LocalDate birthdate;
public Person(String name, String role, String city, LocalDate birthdate) {
super();
this.name = name;
this.role = role;
this.city = city;
this.birthdate = birthdate;
}
public boolean isFrom(String name) {
return city.equals(name);
}
public String getRole() {
return role;
}
@Override
public String toString() {
return "Stakeholder [name=" + name + ", role=" + role + ", city=" + city + ", birthdate=" + birthdate + "]";
}
public boolean isOlderThan(Person other) {
return birthdate.isBefore(other.birthdate);
}
public boolean isYoungerThan(Person other) {
return birthdate.isAfter(other.birthdate);
}
/**
* Questa è una funzione di ordine superiore (higher-order function) perché restituisce una funzione
*
*/
public static Function<Person, LocalDate> toBirthdate() {
return p -> p.birthdate;
}
public LocalDate getBirthdate() {
return birthdate;
}
public String getName() {
return name;
}
@Override
public int compareTo(Person o) {
return name.compareTo(o.name);
}
public String getCity() {
return city;
}
public static List<Person> makeList() {
List<Person> persons = new ArrayList<>();
persons.add(new Person("Paolo Bianchi", "Tecnico", "Milano", LocalDate.of(1960, 4, 15)));
persons.add(new Person("Mario Rossi", "Tecnico", "Milano", LocalDate.of(1981, 3, 25)));
persons.add(new Person("Lucia Verdi", "Tecnico", "Milano", LocalDate.of(1955, 5, 1)));
persons.add(new Person("Marzia Gentile", "Commerciale", "Milano", LocalDate.of(1969, 6, 3)));
persons.add(new Person("Anna De Rosa", "Responsabile", "Milano", LocalDate.of(1974, 4, 22)));
persons.add(new Person("Paola De Blasi", "Tecnico", "Torino", LocalDate.of(1967, 8, 11)));
persons.add(new Person("Maria Abate", "Tecnico", "Torino", LocalDate.of(1977, 10, 21)));
persons.add(new Person("Federico Cacciari", "Commerciale", "Torino", LocalDate.of(1962, 12, 17)));
persons.add(new Person("Arturo Pace", "Commerciale", "Roma", LocalDate.of(1979, 10, 19)));
persons.add(new Person("Giovanni Tomassini", "Commerciale", "Genova", LocalDate.of(1974, 9, 8)));
return persons;
}
}
因此,我创建了另一个名为“InternalIterator.java”的类,目的是尝试沿着前一个类的方法“makeList()”创建的集合使用 Streams。在流内部,我尝试调用“isFrom()”方法,但就像找不到该方法一样:
Cannot resolve method 'isFrom(java.lang.String)
这很奇怪,因为我已经在 Person 类中实现了该方法。第二类的代码如下所示:
import java.util.List;
import ardea.project.Person;
public class InternalIterator {
public static void main(String[] args) {
List persons = Person.makeList();
long count = persons.stream()
.filter(p -> p.isFrom("Milano"))
.count();
System.out.println("Persone di Milano: " + count);
}
}
我不明白,我也尝试过 Person.isFrom()
但无事可做
最佳答案
原因是您使用的是原始类型。
List persons = Person.makeList();
因此,当您从 persons
创建流时,类型会被推断为对象。
相反,这样做
List<Person> persons = Person.makeList();
推荐阅读:
关于java - 无法使用 Stream 和 Lambda 从另一个类导入的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50414020/