मुझे एक बिल्डर लाइब्रेरी की आवश्यकता है जिसे स्कैला और जावा से बुलाया जा सकता है। डिफ़ॉल्ट, नाम पैरामीटर का उपयोग कर स्कैला में काफी आसान है। लेकिन मैं इस कोड को जावा से कैसे कॉल करूं? निचे देखो। या शायद मुझे एक धाराप्रवाह एपीआई के साथ जाना चाहिए जो दोनों भाषाओं के लिए अधिक आम है?स्केल और जावा के लिए बिल्डर लाइब्रेरी
स्काला:
case class Person(gender:Gender.Value, firstName:String, lastName:String){
def fullName = lastName+", "+firstName
override def toString = firstName+","+lastName+","+gender
}
case class PersonBob(
gender:Gender = GenderBob().build,
firstName:String = null,
lastName:String = null) {
def build = Person(
gender,
if(firstName == null) NameBob(if(gender==Gender.Male) engMaleNames
else engFemaleNames).build else firstName,
if(lastName==null) NameBob(engLastNames).build
else lastName
)
}
उपयोग:
val p1 = PersonBob().build
val p2 = PersonBob(lastName = "Jones").build
जावा उपयोग:
Person p1 = ?
Person p2 = ?
के लिए कम वर्बोज़ हो सकता है ध्यान दें कि मैंने एक समान लेकिन अलग प्रश्न बनाया है प्रकार-सुरक्षित निर्माता के संबंध में। देखें [http://stackoverflow.com/questions/6347083/type-safe-buider-library-for-scala-and-java ](httpoverstow.com/questions/6347083/type-safe-buider-library -किसी-स्केला और जावा) – eptx