posts - 6,  comments - 7,  trackbacks - 0
            2008年3月4日
           1 package cn.com.gentek.imatrix.test;
           2 
           3 public class tesRef {
           4     private DataItem item1;
           5     private DataItem item2;
           6 
           7     public tesRef() {
           8         item1 = new DataItem();
           9         item2 = item1;
          10     }
          11 
          12     public void newItem1() {
          13         item1 = new DataItem();
          14     }
          15 
          16     public void print() {
          17         System.out.println("item1: " + item1.toString());
          18         System.out.println("item2: " + item2.toString());
          19     }
          20 
          21     public static void main(String[] args) {
          22         tesRef tr = new tesRef();
          23         tr.print();
          24         tr.newItem1();
          25         tr.print();
          26     }
          27 }
          28 


              以上一段很簡(jiǎn)單的代碼,很容易看懂。它的運(yùn)行結(jié)果如下:
          item1: cn.com.gentek.imatrix.test.DataItem@c17164
          item2: cn.com.gentek.imatrix.test.DataItem@c17164
          item1: cn.com.gentek.imatrix.test.DataItem@1fb8ee3
          item2: cn.com.gentek.imatrix.test.DataItem@c17164

              toString()的結(jié)果格式為類名@對(duì)象的16進(jìn)制Hash表示。這里我們可以如此理解,是一個(gè)指向DataItem類實(shí)例化時(shí),在內(nèi)存中開辟的一塊空間的地址標(biāo)識(shí)。
              在調(diào)用函數(shù)tr.newItem1()(24行)之前,item1和item2所指向的內(nèi)存空間是相同的。所以在改變item1的同時(shí)item2的值勢(shì)必更這一起改變,同理改變item2的內(nèi)容,item1的內(nèi)容也會(huì)做出相同的改變。item1.toString()和item2.toString()的結(jié)果正可以說(shuō)明這一點(diǎn)。這也說(shuō)明了,item1和item2存儲(chǔ)的都是一個(gè)內(nèi)存地址。
              當(dāng)調(diào)用
          tr.newItem1(),重新實(shí)例化item1,之后item1指向的另一塊內(nèi)存空間,而item2保持不變,指向最初那塊內(nèi)存空間。此時(shí),item1和和item2的內(nèi)容將是毫不相關(guān)的。

          posted @ 2008-03-04 17:33 zhan 閱讀(1591) | 評(píng)論 (2)編輯 收藏
            2008年3月3日

          1.       HTML代碼

          最終實(shí)現(xiàn)的效果代碼,如下所示:

          <select>

          <option selected="selected" value="Monitor">Monitor</option>

          <option value="VCR">VCR</option>

          <option value="Standard Device">Standard Device</option>

          <option value="Smart Device">Smart Device</option>

          <option value="Trunk">Trunk</option>

          <option value="Standby VCR">Standby VCR</option>

          </select>

          2.       enum代碼

          publicenum DeviceType {

              @XmlEnumValue("Monitor")

              MONITOR("Monitor"),

              VCR("VCR"),

              @XmlEnumValue("Standard Device")

              STANDARD_DEVICE("Standard Device"),

              @XmlEnumValue("Smart Device")

              SMART_DEVICE("Smart Device"),

              @XmlEnumValue("Trunk")

              TRUNK("Trunk"),

              @XmlEnumValue("Standby VCR")

              STANDBY_VCR("Standby VCR");

              privatefinal String value;

              DeviceType(String v) {

                  value = v;

              }

              public String value() {

                  returnvalue;

              }

              publicstatic DeviceType fromValue(String v) {

                  for (DeviceType c: DeviceType.values()) {

                      if (c.value.equals(v)) {

                          return c;

                      }

                  }

                  thrownew IllegalArgumentException(v);

              }

          }

          3.       JSF標(biāo)簽:

          <h:selectOneMenu value="#{voutputType.DEVICETYPE}" converter="voutputDeviceTypeConverter">

          <f:selectItems value="#{voutput.deviceTypeList}"/>

          </h:selectOneMenu>

          主要有三個(gè)部分組成

          (a)     value="#{voutputType.DEVICETYPE}"

          javabean ,voutputType中的DEVICETYPE屬性,確定html代碼中<option selected="selected" value="Monitor">項(xiàng)的值

          voutputType配置信息在"WebRoot"WEB-INF"faces-config.xml

          <managed-bean>

                 <managed-bean-name>voutputType</managed-bean-name>

                 <managed-bean-class>

                     cn.com.gentek.imatrix.xml.jaxb.voutput.ObjVOutputType

                 </managed-bean-class>

                 <managed-bean-scope>session</managed-bean-scope>

          </managed-bean>

              其中DEVICETYPE屬性對(duì)應(yīng)的變量是枚舉DeviceType的一個(gè)實(shí)例。

          (b)    converter="voutputDeviceTypeConverter"

          類型轉(zhuǎn)換器,在在"WebRoot"WEB-INF"faces-config.xml配置如下:

          <converter>

              <converter-id>voutputDeviceTypeConverter</converter-id>

              <converter-class>

                 cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter

              </converter-class>

          </converter>

          cn.com.gentek.imatrix.ui.VoutDeviceTypeConverter代碼如下:

          (實(shí)現(xiàn)< select><option>String類型值,與DeviceType類型之間的轉(zhuǎn)換)

          publicclass VoutDeviceTypeConverter implements Converter {

              public Object getAsObject(FacesContext context, UIComponent component, String value) {

                 DeviceType result = null;

                 if (value == null || value.length() < 1) {

                     result = null;

                 } else

                     result = DeviceType.fromValue(value);

                 returnresult;

              }

              public String getAsString(FacesContext context, UIComponent component, Object value) {

                 String result = null;

                 if (value != null) {

                     if (value instanceof DeviceType) {

                        DeviceType temp = (DeviceType) value;

                        result = temp.value();

                     }

                 }

                 return result;

              }

          }

          (c)      <f:selectItems value="#{voutput.deviceTypeList}"/>(重點(diǎn))

          由于deviceTypeList對(duì)應(yīng)變量必須是SelectItemjavax.faces.model.SelectItem)列表,所以有必要將DeviceType類型實(shí)例的值和對(duì)應(yīng)String類型值,封裝在一個(gè)SelectItem實(shí)例中。實(shí)現(xiàn)代碼如下:

          ArrayList<SelectItem> deviceTypeList = new ArrayList<SelectItem>();

          for (int i = 0; i < DeviceType.values().length; i++) {

              deviceTypeList.add(new SelectItem(DeviceType.values()[i],

                     DeviceType.values()[i].value()));

          }

          posted @ 2008-03-03 16:15 zhan 閱讀(2130) | 評(píng)論 (3)編輯 收藏
            2008年2月29日

          使用JSF編寫web程序的時(shí)候,JavaBean無(wú)法直接通過(guò)相對(duì)路徑來(lái)訪問文件。經(jīng)過(guò)一天的研究主要發(fā)現(xiàn)兩類解決方案,一是,通過(guò)FacesContext,二是,通過(guò)ClassLoader

          下面通過(guò)實(shí)例來(lái)說(shuō)明。

          首先是介紹web程序目錄的大致結(jié)構(gòu):

          D:"......"Tomcat 6.0"webapps"imatrixb ------> 程序的更目錄

                   --META-INF

                   --WEB-INF

                   ---------------classess

                   ---------------------------cn

                   ----------------------------------com

                   --------------------------------------------……                        ----------->class 文件

                   ---------------------------XmlData

                   ---------------------------------path-config.xml           1

                            --------------- path-config.xml                                           2

          …….

          Index.jsp

          一:FacesContext

          獲得(2)號(hào)path-config.xml文件信息,

          代碼如下:

          String partPath=”/ WEB-INF/ path-config.xml”;

          1. getRealPath():

          FacesContext context = FacesContext.getCurrentInstance();
          HttpServletRequest rst = (HttpServletRequest)context.getExternalContext().getRequest();
          String fullPath=rst.getRealPath(xmlfile); //
          獲得xml文件的系統(tǒng)路徑,xmlfile為相對(duì)路徑

                   采用這個(gè)方法存在一些隱患:比方說(shuō)對(duì)一個(gè)打包的應(yīng)用來(lái)說(shuō),是沒有RealPath的概念的,調(diào)用getRealPath只會(huì)簡(jiǎn)單地返回null

                   2. getResourceAsStream():

          FacesContext context = FacesContext.getCurrentInstance();

          InputStream xmlStream = context.getExternalContext()

                        .getResourceAsStream(xmlfile);

              用于只讀的形式。

              二:ClassLoader

          獲得(1)號(hào)path-config.xml文件信息,

          代碼如下:

          String partPath =”/XmlData/path-config.xml”;

          String fullPath=this.getClass().getClassLoader().getResource(partPath).getPath();

          //使用的時(shí)候還是存在一些問題,無(wú)法正常使用,暫時(shí)沒有發(fā)現(xiàn)解決的辦法

          InputStream xmlStream=this.getClass().getClassLoader().getResourceAsStream(partPath);

                   //用于只讀的形式下,通過(guò)測(cè)試能夠正常的使用

          posted @ 2008-02-29 17:36 zhan 閱讀(2105) | 評(píng)論 (2)編輯 收藏
            2008年2月26日

          1.2 反射

          1.2.1 學(xué)習(xí)筆記

          參考資料:Java 2 核心技術(shù)I:基礎(chǔ)知識(shí)(第7版) 5.5 反射

          (1) Class

              在程序運(yùn)行期間,Java運(yùn)行時(shí)系統(tǒng)始終為所有對(duì)象的維護(hù)一個(gè)被稱為運(yùn)行時(shí)的類型標(biāo)識(shí)。這個(gè)信息保存著每一個(gè)對(duì)象所有屬性的類足跡。虛擬機(jī)利用運(yùn)行信息選擇相應(yīng)的方法執(zhí)行。

          獲取Class類對(duì)象的三種方法

          (a)     getClass()

                   Employee e;

                   …

                   Class cl=e.getClass();

                   System.out.println(cl.getName()+“  ” +e.getName());

           Result:

                   Employee Harry

          (b)    forName()

          String className= “java.util.Date ”;

          Class cl=Class.forName(className);

          (c)    .class

            Class cl1=Date.class;

           Class cl2=int.class;

          (2) 反射的分析能力

           示例:

          Employee.java:

          publicclass Employee {

              private String name;

              privateintage;

              public String getName() {

                  returnname;

              }

              publicvoid setName(String name) {

                  this.name = name;

              }

              publicint getSalary() {

                  returnage;

              }

              publicvoid setSalary(int salary) {

                  this.age = salary;

              }

              public Employee(String name, int salary) {

                  this.name = name;

                  this.age = salary;

              }

          }

          Test.java

          import java.lang.reflect.Field;

          publicclass test {

                   publicstaticvoid main(String[] args) throws SecurityException,

                                      NoSuchFieldException, IllegalArgumentException,

                                      IllegalAccessException {

                             Employee zhanjh = new Employee("zhan jh", 1000);

                             Class<?> cl = zhanjh.getClass();

                             Field f = cl.getDeclaredField("name"); // 返回名稱為“name”的私有或公有成員(域)

                             f.setAccessible(true); // 非常重要,否則無(wú)法調(diào)用f.get(zhanjh)方法

                             Object v = f.get(zhanjh);// 返回zhanjh對(duì)象中 name成員(域)的值

                             System.out.println(v.toString());

                   }

          }

          /*

           * 運(yùn)行結(jié)果: zhan jh

           */

          posted @ 2008-02-26 17:09 zhan 閱讀(161) | 評(píng)論 (0)編輯 收藏
            2008年2月25日
               摘要: 從去年12月份還是學(xué)習(xí)Java到現(xiàn)在已經(jīng)將近3個(gè)月了,現(xiàn)在已經(jīng)很有必要對(duì)以前所學(xué)的知識(shí)進(jìn)行一次系統(tǒng)的復(fù)習(xí)。而重新復(fù)習(xí)最好的辦法就是將最近剛完成,但不完善的Xml數(shù)據(jù)配置的Web程序,進(jìn)行一次重構(gòu)。 其中需要重新復(fù)習(xí)的知識(shí)主要內(nèi)容如下: 1.             ...  閱讀全文
          posted @ 2008-02-25 17:09 zhan 閱讀(1107) | 評(píng)論 (0)編輯 收藏
            2007年12月26日
          interfaces

          上午完成thinking Java中關(guān)于Interfaces章節(jié)的內(nèi)容。下面是該章節(jié)中關(guān)于"Interfaces and factories"的例子
          package com.zhanjh.thinkingjava.interfaces;

          interface Service{
              void method1();
              void method2();
          }

          interface ServiceFactory{
              Service getService();
          }

          class Implementation1 implements Service{
              public Implementation1() {
                  // TODO Auto-generated constructor stub
              }
             
              public void method1(){
                  System.out.println("Implementation1 method1");
              }
              public void method2(){
                  System.out.println("Implementation1 method2");
              }
          }

          class Implementation1Factory implements ServiceFactory{
              public Service getService(){
                  return new Implementation1();
              }
          }

          class Implementation2 implements Service{
              public Implementation2() {
                  // TODO Auto-generated constructor stub
              }
             
              public void method1(){
                  System.out.println("Implementation2 method1");
              }
              public void method2(){
                  System.out.println("Implementation2 method2");
              }
          }

          class Implementation2Factory implements ServiceFactory{
              public Service getService(){
                  return new Implementation2();
              }
          }

          public class Factories{
              public static void serviceConsumer(ServiceFactory fact){
                  Service s=fact.getService();
                  s.method1();
                  s.method2();
              }
              public static void main(String[] args){
                  serviceConsumer(new Implementation1Factory());
                  serviceConsumer(new Implementation2Factory());
              }
          }
          總結(jié):abstract class和interface是Java語(yǔ)言中對(duì)于抽象類定義進(jìn)行支持的兩種機(jī)制,abstract class和interface之間在對(duì)于抽象類定義的支持方面具有很大的相似性。目前我對(duì)他們區(qū)分的方法大致如下:
          1)interface可以多重實(shí)現(xiàn),而abstract class只能單一繼承
          2)abstract class不一定只有抽象的方法(abstract method),它也可以包含具體的方法(concrete method)。而interface不能包含方法的實(shí)現(xiàn)(implementation)。所以在程序設(shè)計(jì)的時(shí)候,能用inteface的時(shí)候盡量不要用abstract class。

          下午
               查找關(guān)于EJB的資料,沒頭緒。
               jaxb入門學(xué)習(xí)。
               xjc(將xsd文件轉(zhuǎn)換為Java的小工具)工具的使用。可以創(chuàng)建一個(gè)bat文件處理下面的命令:
              xjc -d "D:"eclipse"workspace"JaxbTest"src" -p "edu.jlu.xml" "D:"eclipse"workspace"JaxbTest"schema"messages.xsd"
          其中D:"eclipse"workspace"JaxbTest"src為原文件的目錄,edu.jlu.xml為生成Java類的包名,D:" eclipse"workspace"JaxbTest"schema"messages.xsd為xml schema文件的路徑。

          posted @ 2007-12-26 19:07 zhan 閱讀(224) | 評(píng)論 (0)編輯 收藏
          僅列出標(biāo)題  
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 青州市| 新竹市| 凤冈县| 浙江省| 繁峙县| 沙洋县| 叶城县| 镇宁| 博客| 绥阳县| 雅江县| 彭泽县| 恭城| 双峰县| 广丰县| 开鲁县| 曲靖市| 仪征市| 腾冲县| 嘉荫县| 涟源市| 千阳县| 永康市| 佛山市| 华池县| 辽宁省| 射阳县| 镇平县| 陕西省| 洛南县| 育儿| 汉阴县| 南江县| 灌阳县| 津市市| 长沙县| 南京市| 斗六市| 林州市| 景德镇市| 沁水县|