Discuss / Java / 为什么我编译运行一直失败The method of(Person, Person, Person) is undefined for the type List

为什么我编译运行一直失败The method of(Person, Person, Person) is undefined for the type List

Topic source

package com.itranswarp.learnjava;

import java.util.*;

import java.util.List;

public class Main {

    public static void main(String[] args) {

        Person a = new Person("Xiao", "Ming", 18);

        Person b = new Person("Xiao", "Hong", 25);

        Person c = new Person("Bob", "Smith", 20);

        List<Person> list = List.of(a, b, c);

        boolean exist = list.contains(new Person("Bob", "Smith", 20));

        System.out.println(exist ? "测试成功!" : "测试失败!");

    }

}

class Person {

    String firstName;

    String lastName;

    int age;

    public Person(String firstName, String lastName, int age) {

        this.firstName = firstName;

        this.lastName = lastName;

        this.age = age;

    }

   public boolean equals(Object o) {

    if (o instanceof Person) {

        Person p = (Person) o;

        return Objects.equals(this.firstName, p.firstName) && Objects.equals(this.lastName, p.lastName) && this.age == p.age;

    }

    return false;

    }

}

上面是代码下面是错误

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    The method of(Person, Person, Person) is undefined for the type List

    at com.itranswarp.learnjava.Main.main(Main.java:11)

灵逗士

#2 Created at ... [Delete] [Delete and Lock User]

同样代码我测试过了没出错,大概是环境的问题

我用的jdk1.8一直不好使,但是重新装了一遍IDE还是同样的配置就好使了,不知道为啥

RickyNg1113

#4 Created at ... [Delete] [Delete and Lock User]

Prior to Java 9, you need to go for Arrays.asList() instead of List.of()


  • 1

Reply