मैं एक PersonFactory
इंटरफेस के रूप में निम्नानुसार है:डिफ़ॉल्ट विधि में स्वत: निर्माता मिलान
@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?
}
}
Person
वर्ग:
public class Person {
public String firstname;
public String lastname;
public Person() {}
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
}
मैं इस तरह मेरी Person
रों का दृष्टांत करने में सक्षम होना चाहता हूँ:
PersonFactory<Person> personFactory = Person::new;
Person p = personFactory.create(); // does not work
Person p = personFactory.create("firstname", "lastname"); // works
क्या कोई तरीका है जिससे मैं जावा सह बना सकता हूं mpiler स्वचालित रूप से PersonFactory.create()
के हस्ताक्षर से मेल करके सही कन्स्ट्रक्टर का चयन करता है?
सं केवल समझदार जिस तरह से आप डिफ़ॉल्ट विधि लागू कर सकते हैं 'this.create करके है (" someDefaultFirstName "," someDefaultLastName ") ; ' –