豆沙包

          …… …… 所學(xué) 所寫 所想 所做 所悟…… ……

          Groovy Beans

          Groovy Beans 是 JavaBeans ,但是用了一種更為簡單的語法.
          ????
          ???? 如下示例:

          import?java.util.Date

          class?Customer?{
          ????
          //?properties
          ????Integer?id
          ????String?name
          ????Date?dob
          ????
          ????
          //?sample?code
          ????static?void?main(args)?{
          ????????customer?
          =?new?Customer(id:1,?name:"Gromit",?dob:new?Date())
          ????????println(
          "Hello?${customer.name}")
          ????}

          }

          結(jié)果:

          Hello Gromit????

          注意,那些properties看起來就象是public fields. 在Groovy里你也可以把properties? 放入Bean的構(gòu)造方法中.在Groovy中,fields和properties已經(jīng)合并故他們用起來并無不同.? 因此,上面的Groovy代碼和下面的Java代碼是相同的:

          import?java.util.Date;

          public?class?Customer?{
          ????
          //?properties
          ????private?Integer?id;
          ????
          private?String?name;
          ????
          private?Date?dob;
          ????
          ????
          public?Integer?getId()?{
          ????????
          return?this.id;
          ????}

          ????
          ????
          public?String?getName()?{
          ????????
          return?this.name;
          ????}

          ????
          ????
          public?Date?getDob()?{
          ????????
          return?this.dob;
          ????}

          ????
          ????
          public?void?setId(Integer?id)?{
          ????????
          this.id?=?id;
          ????}

          ????
          ????
          public?void?setName(String?name)?{
          ????????
          this.name?=?name;
          ????}

          ????
          ????
          public?void?setDob(Date?dob)?{
          ????????
          this.dob?=?dob;
          ????}

          ????
          ????
          //?sample?code
          ????public?static?void?main(String[]?args)?{
          ????????Customer?customer?
          =?new?Customer();
          ????????customer.setId(
          1);
          ????????customer.setName(
          "Gromit");
          ????????customer.setDob(
          new?Date());
          ???????
          ????????println(
          "Hello?"?+?customer.getName());
          ????}

          }


          Property和field規(guī)則????
          當(dāng)Groovy程序被編譯成字節(jié)碼,下面的規(guī)則將被遵循.????
          ?????
          * 如果property是private的,那么就用一個Java field來表示.????
          * 如果聲明了一個public或者protected的property(properties缺省是public的),?? 那么一個public或者protected的getter和setter與相應(yīng)的private的Java field一同被創(chuàng)建出來.????
          * 如果你沒有為public或者proteced的properties顯示地聲明getter與setter,他們一樣會在字節(jié)碼一級被自動創(chuàng)建.????
          * 如果你創(chuàng)建了一個public或者protected的property,那么你可以重載那些被自動創(chuàng)建的方法. 舉一個例子,你能夠創(chuàng)建一個只讀的property或者一個帶有protected setter的???? public read-only property,如下:

          class?Foo?{
          ????
          //?read?only?property
          ????private?String?name
          ????
          public?String?getName()?{?return?name?}
          ????
          ????
          //?read?only?property?with?protected?setter
          ????Integer?amount
          ????
          protected?void?setAmount(Integer?amount)?{?this.amount?=?amount?}
          ????
          ????
          //?dynamically?typed?property
          ????cheese
          }

          posted on 2005-02-24 09:49 carob 閱讀(500) 評論(0)  編輯  收藏 所屬分類: Groovy


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 平潭县| 二手房| 诏安县| 舟曲县| 台东市| 柞水县| 哈巴河县| 永清县| 怀来县| 彭州市| 安平县| 铜山县| 西城区| 丹江口市| 葫芦岛市| 体育| 桂林市| 禄劝| 赣榆县| 双桥区| 井陉县| 内丘县| 临武县| 安仁县| 英山县| 金堂县| 高州市| 龙南县| 广宗县| 汉阴县| 太康县| 罗源县| 高清| 乃东县| 杨浦区| 策勒县| 寻乌县| 尉犁县| 天长市| 拜城县| 丰县|