我想參數保存爲一個屬性,參數爲:testSpeed (= "20")
,我有一個方法:如何獲取Java中參數的字符串值?
saveProperty(String Parameter)
將參數的值保存到一個文件中,這樣的事情:
saveProperty(testSpeed);
我的屬性文件看起來是這樣的:
testSpeed : 20
testHeight : 300
我的問題是:在Java中是有可能得到的參數的名稱在這種情況下傳遞給saveProperty()
參數是testSpeed
,它的名字是"testSpeed"
,如何從saveProperty()
內得到這個字符串,所以我不必做以下保存:
saveProperty(String Name,String Value);
和這樣稱呼它:
saveProperty("testSpeed",testSpeed)
此外,當我需要從屬性文件把它弄出來,我可以打電話:
getProperty(testSpeed)
我知道如何使用屬性文件,我只是用它作爲例子,也許它引起了一些混淆,當我提到屬性文件。
問題的實質是:如何獲取傳入方法的參數的名稱。
void someMethod(String Parameter_XYZ)
{
// In here if I call someMethod(testSpeed), how to get the string "testSpeed",
// not it's value, but it's name,
// exactly spelled as "testSpeed" ?
}
這是可行的在Java中?
你需要爲'testHeigh'分開設置getter/setter(這當然不推薦用於100個屬性的屬性文件) – anubhava
@ Alya'aGamal:在這裏如何使用正則表達式? – anubhava
@ Alya'aGamal:我認爲這只是一個例子來說明testSpeed的價值。 OP想要使用這個代碼:**'testSpeed = 20; saveProperty(testSpeed);'** – anubhava