ฉันต้องการแทรกค่าให้กับตัวแปร Object
โดยไม่ใช้ตัวตั้งค่า เป็นไปได้อย่างไร..
นี่คือตัวอย่าง
Class X{
String variableName;
// getters and setters
}
ตอนนี้ฉันมีฟังก์ชันที่ประกอบด้วย variable name
, value to be set
และ Object of the Class X
ฉันกำลังพยายามใช้วิธีการทั่วไปเพื่อตั้งค่าเป็น Object(objectOfClass) ด้วยค่าที่ฉันส่งผ่าน (valueToBeSet
) ในตัวแปรที่เกี่ยวข้อง (variableName
)
Object functionName(String variableName, Object valueToBeSet, Object objectOfClass){
//I want to do the exact same thing as it does when setting the value using the below statement
//objectOfClass.setX(valueToBeSet);
return objectOfClass;
}