Tôi có một giao diện PersonFactory
như sau:hợp nhà xây dựng tự động trong phương pháp mặc định
@FunctionalInterface
public interface PersonFactory<P extends Person> {
P create(String firstname, String lastname);
// Return a person with no args
default P create() {
// Is there a way I could make this work?
}
}
Các Person
lớp:
public class Person {
public String firstname;
public String lastname;
public Person() {}
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
Tôi muốn để có thể nhanh chóng Person
s của tôi như thế này:
PersonFactory<Person> personFactory = Person::new;
Person p = personFactory.create(); // does not work
Person p = personFactory.create("firstname", "lastname"); // works
Có cách nào tôi có thể làm cho đồng Java không mpiler tự động chọn hàm tạo đúng bằng cách khớp với chữ ký của PersonFactory.create()
?
số Cách duy nhất hợp lý bạn có thể thực hiện phương pháp mặc định là bằng cách làm 'this.create ("someDefaultFirstName", "someDefaultLastName") ; ' –