我有一個bean類名稱爲「Bean1」。在我的主要方法中,我得到了一個包含變量名稱的字符串。 String str =「Bean1」;現在我該如何使用String變量來獲取類並訪問Bean屬性。我是Java新手。請幫忙。java String to class
回答
您應該使用Java反射API:
Class c = Class.forName("package.name.Bean1");
然後你可以使用c.newInstance()實例類。此方法使用不需要參數的構造函數。
在這裏看到的細節:http://download.oracle.com/javase/tutorial/reflect/
他可能還需要一個Bean1的實例 – cherouvim 2010-10-27 06:52:57
我試圖實現的功能是「有一個Inetrface和相關的Bean類在運行時間的應用程序。我能夠獲得bean類和接口所在的路徑。現在我有一個類「SrcInfo」,其中Bean的mehtod列表和參數作爲類變量提供。現在我必須動態創建一個java類,我需要創建一個將Bean對象作爲參數的方法,並通過操作Bean對象中包含的數據來返回HashMap。因此,我得到了bean類的名稱作爲字符串變量。 – 2010-10-27 08:06:44
@MANU SINHA:你應該在問題中包含這些信息(請編輯它)。這是獲取有用答案的重要信息 – 2010-10-27 08:20:18
Does Java support variable variables?
爪哇複製不支持動態獲取基於其名的字符串變量(也稱爲可變的變量)。有可能採取不同的方式來做你想做的事情,比如使用Map對象將名字映射到bean。如果你編輯你的問題以更詳細地解釋你想要做什麼,我們可能會有更具體的答案。
(在另一方面,如果這個問題是關於一個叫Bean1,然後凱爾的權利類)。
循序漸進:
//1. As Kel has told you (+1), you need to use
//Java reflection to get the Class Object.
Class c = Class.forName("package.name.Bean1");
//2. Then, you can create a new instance of the bean.
//Assuming your Bean1 class has an empty public constructor:
Object o = c.newInstance();
//3. To access the object properties, you need to cast your object to a variable
// of the type you need to access
Bean1 b = (Bean1) o;
//4. Access the properties:
b.setValue1("aValue");
對於最後這一步,你需要知道的bean的類型或者需要訪問的屬性的超類型。如果你對班上的所有信息都是一個帶有名字的字符串,那麼我猜你不知道它。
使用反射,您可以訪問類的方法,但在這種情況下,您需要知道要調用的方法的名稱和輸入參數類型。 與示例走在前面,更改步驟3和4:
// 3. Get the method "setValue1" to access the property value1,
//which accepts one parameter, of String type:
Method m=c.getMethod("setValue1", String.class);
// 4. Invoke the method on object o, passing the String "newValue" as argument:
m.invoke(o, "newValue");
也許你需要重新考慮你的設計,如果你沒有這些信息在運行時avalaible。
- 1. Java String to unicode
- 2. Renjin/Java - Vector class to Java array class
- 3. Java String to Array
- 4. Java String to JS
- 5. Java string to xml to list
- 6. java String to a object
- 7. java string to utc date
- 8. Java String to Date,ParseException
- 9. Java Stream to String [] Array
- 10. Draw to custom buffer class [Java]
- 11. Java Set <Set<String>> to String [] []
- 12. Object(KeyEvent key)to String(Java)
- 13. Java | Json String to Object without Library
- 14. Base64 String to Json String
- 15. NSAttribute string to HTML
- 16. convert class to parcelable
- 17. ArrayBuffer to String,String to ArrayBuffer方法
- 18. VB.Net Class to CSV
- 19. Class digram to Use
- 20. EditText to String? Android
- 21. Android ClassCastException Object [] to String []
- 22. System :: String^to TCHAR *
- 23. String to BigDecimal
- 24. Cast String to TextView
- 25. Jackson:Object to String via writeValueAsString:NPE
- 26. base 64 string to hexa string
- 27. std :: string xml string to object
- 28. Angular2:Subject <string> to string
- 29. c#string [] to jquery string list?
- 30. ICollection <string> to string []
我試圖實現的功能是「在應用程序的運行時創建了一個Inetrface和相關的Bean類,我能夠獲得bean類和接口所在的路徑。得到了一個類「SrcInfo」,其中Bean的mehtod列表和參數作爲類變量提供,現在我必須動態創建一個java類,我需要創建一個方法,它將Bean對象作爲參數並返回一個HashMap操縱Bean對象中包含的數據,因此我得到了Bean類的名稱作爲字符串變量 – 2010-10-27 08:07:01