I have an object (Adult
) with another object (Child
) as a parameter. I am trying to write a Function
that will return the child's name if given the adult.
I wrote this:
public static void main(String[] args) {
Function<Adult, Object> adult_name_f = Adult::getName;
Function<Adult, Object> adult_child_f = Adult::getChild;
Function<Adult, Object> child_name_f = Adult::getChild.getName;
}
static class Adult {
String name;
Child child;
public Child getChild() {
return child;
}
public String getName() {
return name;
}
}
static class Child {
String name;
public String getName() {
return name;
}
}
but (obviously) Adult::getChild.getName
is not a valid Function
.
Is there a way to return the name of the child if given the adult?