如何使用biconsumer来接受一个包含对象、字符串列表和变量的对象?

llmtgqce  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(244)

我尝试使用biconsumer来接受一个包含变量的对象,一个对象和java中的字符串列表。如果只使用双消费者,我不知道如何将值设置为一个对象。也许,如果我尝试将student对象 Package 在一个列表中并将其传递给一个新student可能会有所帮助,但到目前为止,我得到了一个空对象。我还没有看到过很多post中的object只包含一个object中的变量并使用biconsumer。

@Test
public void testStudent() {
    List<Object> objectList1 = new ArrayList<>();
    Student student = new Student();

    StudentLevel studentLevel = new StudentLevel("freshman", true);
    List<String> studentLists = Arrays.asList("Maria", "Jose", "Juan");
    Student student1 = new Student("Maria", "Lopez", "A", studentLevel, studentLists);
    objectList1.add(student1);

    BiConsumer<Object, List<Object>> biconsumer = (obj, list) -> {
      for (Object object: list) {
        // set and get but how?
        // obj = object;
      }
    };

    // To accept values from the object list see below for desired output
    biconsumer.accept(student, objectList1); 

    // For debugging purpose
    System.out.println("All Student: " + student);
}
public class Student {

    private String name;
    private String lastName;
    private String grade;
    private StudentLevel studentGrade;
    private List<String> studentList;

    public Student(final String name, final String lastName, final String grade, final StudentLevel studentGrade, final List<String> studentList) {
        this.name = name;
        this.lastName = lastName;
        this.grade = grade;
        this.studentGrade = studentGrade;
        this.studentList = studentList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public StudentLevel getStudentGrade() {
        return studentGrade;
    }

    public void setStudentGrade(StudentLevel studentGrade) {
        this.studentGrade = studentGrade;
    }

    public List<String> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<String> studentList) {
        this.studentList = studentList;
    }
}
public class StudentLevel {

    private String level;
    private Boolean pass;

    public StudentLevel(final String level, final Boolean pass){
        this.level = level;
        this.pass = pass;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public Boolean getPass() {
        return pass;
    }

    public void setPass(Boolean pass) {
        this.pass = pass;
    }
}

期望输出:

student = {Student@887} 
  name = "Maria"
  lastName = "Lopez"
  grade = "A"
  studentGrade = {StudentLevel@889} 
    level = "freshman"
    pass = {Boolean@906} true
  studentList = {Arrays$ArrayList@890}  size = 3
    0 = "Maria"
    1 = "Jose"
    2 = "Juan"
lpwwtiir

lpwwtiir1#

您正在分配的本地引用 objectobj (不会复制值)

obj = object; // means, student = object

在使用者的范围之外不会反映任何更改,相反,您需要使用setters修改状态,如下所示:

((Student) obj).setName(((Student) object).getName());
obj = object; // after this point, student object won't be accessible in the current scope.

注意:应该有getter和setter来访问外部的私有属性 Student 类,这个例子只是演示了通过分配 name 财产。
参考文献:
•类和对象

相关问题