代码之家  ›  专栏  ›  技术社区  ›  Giacomo Brunetta

不能使用从另一个使用流和lambda的类导入的方法

  •  0
  • Giacomo Brunetta  · 技术社区  · 6 年前

    有点奇怪:我不能使用导入类中实现的方法。 我有一个名为“.java.”的类,它包含一个实现的方法“iSo()”,如下所示:

    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;
      }
    
    }
    

    因此,我创建了另一个名为“IntualIdult.java”的类,目的是尝试使用前一个类的方法“MaKeStId()”创建的集合中的流。在流中,我尝试调用“isFrom()”方法,但如果它找不到该方法:

    无法解析方法“来自”(java.

    这很奇怪,因为我已经在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() 但没什么可做的

    2 回复  |  直到 6 年前
        1
  •  2
  •   Thiyagu    6 年前

    原因是你使用的是 原始类型 .

    List persons = Person.makeList();
    

    因此当你创造出 persons 类型被推断为 对象 .

    相反,应该

    List<Person> persons = Person.makeList();
    

    推荐阅读:

    What is a raw type and why shouldn't we use it?

        2
  •  1
  •   Abder KRIMA    6 年前

    使用此: List<Person> persons = Person.makeList();