Groovy – Object property 實驗

class MyClass{
  String Name
}
def my = new MyClass(Name: "Hello")
assert my.Name == "Hello"
assert my.getProperty("Name") == "Hello"
assert my.getProperty("name") == "Hello"
assert my.getProperties().get("Name") != "Hello"
assert my.getProperties().get("name") == "Hello"
  • getProperties() 會回傳一個 Map, key 為屬性名稱, value 為屬性值.
  • getProperties() 回傳的 Map 裡, 已經將屬性名稱經過 java naming rule 的調整, 因此取值要以 naming rule 的名稱為 key
  • getProperty() 可使用原來的名稱, 也可以用調整後的名稱.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s