网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  在eclipse 3.1体验j2se 5.0新特性五(图)     
  文章作者:未知  文章来源:水木森林  
  查看:87次  录入:管理员--2007-11-17  
 
  清单19 vendor的类定义
  
  public class vendor extends employee {
  @exportable string name;
  @exportable string company;
  @exportable string team;
  @exportable string workinghours;
  string empno;
  
  public vendor(string name, string company, string team, string hours) {
  super();
  this.name = name;
  this.company = company;
  this.team = team;
  workinghours = hours;
  }
  }
  
  清单20 contractor的类定义
  
  public class contractor extends employee{
  @exportable string name;
  @exportable string company;
  @exportable string contractduration;
  string empno;
  
  public contractor(string name, string company) {
  super();
  // todo auto-generated constructor stub
  this.name = name;
  this.company = company;
  contractduration ="1";
  }
  }
  
  清单21 supplemental的类定义
  
  public class contractor extends employee{
  @exportable string name;
  @exportable string company;
  @exportable string contractduration;
  string empno;
  
  public contractor(string name, string company) {
  super();
  this.name = name;
  this.company = company;
  contractduration ="1";
  }
  }
  清单22使用exportablegenerator的程序
  
  public class testexportable {
  public testexportable() {
  super();
  }
  public static void main(string[] args) {
  regular em=new regular
  ("catherine","ibm","software engineer","82888288","bj", new date());
  employee vn1=new vendor("steve","ibm","pvc","8");
  employee vn2=new vendor("steve","ibm","pvc","8");
  employee ct=new contractor("joe","ibm");
  employee sup=new supplemental("linda","ibm","8");
  em.addmemeber(vn1);
  em.addmemeber(vn2);
  em.addmemeber(ct);
  em.addmemeber(sup);
  
  printwriter ps;
  try {
  ps = new printwriter(new fileoutputstream
  (new file("c://test.output"),true));
  exportablegenerator eg=new txtexportablegenerator(ps);
  eg.gendoc(em,0);
  eg.flush();
  } catch (filenotfoundexception e) {
  e.printstacktrace();
  }
  }
  }
  
  清单23 exportablegenerator
  
  public abstract class exportablegenerator {
  printwriter out = null;
  public exportablegenerator(printwriter out) {
  super();
  this.out = out;
  }
  public void gendoc(employee e, int tagnum) {
  
  class employee = e.getclass();
  field[] fields = employee.getdeclaredfields();
  outputfieldheader(out,e);
  for (field f : fields) {
  if (f.isannotationpresent(exportable.class)) {
  if (f.gettype() != arraylist.class) {
  for(int i=0; i
  
  清单24 txtexportablegenerator
  
  public class txtexportablegenerator extends exportablegenerator {
  
  public txtexportablegenerator(printwriter out) {
  super(out);
  }
  
  @override
  protected void outputsimplefield(printwriter out, field f,object obj) {
  out.print(f.getname());
  out.print("=");
  out.print(value(f,obj));
  out.print(";");
  out.println();
  }
  @override
  protected void outputfieldheader(printwriter out,object e) {
  }
  
  @override
  protected void outputfieldfooter(printwriter out,object e) {
  //out.println(e.getclass().getname()+":");
  }
  }
  
  在这个例子中,我们将一个employee对象的部分内容输出到文件c:/test.output中。图8显示了这个例子的输出结果。
  
在eclipse 3.1体验j2se 5.0新特性五(图)

  
图8 输出结果

  
  通过这种方法,我们可以动态生成employee对象的域输出,而不需要在程序中写明要输出哪些确定的域。如果需要更为丰富的格式,我们可以定义多个注释类型。通过对不同注释以及属性的解析,实现格式化的文件输出。
  
  2.4注释类型的小结
  
  所谓元数据,指的是关于信息的信息。一般而言,代码分析工具,测试工具或者部署工具会使用元数据来产生配置信息以及使用配置信息产生控制逻辑。这些工具通常使用java的反射特性,重构元数据的信息,并对这些信息进行解释。
  
  新的技术会不断改变程序设计和开发人员的设计思想。那么注释(annotation)给我们带来了什么呢? 仅仅在代码分析,或者是开发测试框架和部署框架的时候才有用么?
  
  我认为并不是这样。从上面的例子可以看出,注释(annotation)的应用范围其实是很广泛的。在我们的应用中充分的利用元数据,可以提高的软件的质量和可维护性。
 
 
上一篇: 在eclipse 3.1体验j2se 5.0新特性六    下一篇: 在eclipse 3.1体验j2se 5.0新特性四(图)
  相关文档
设计模式:bridge模式 和composite模式 01-07
条件编译 11-16
servlet的图象处理技巧 11-17
图解jbuilderx安装与实践 11-17
玩转java的classpath(二)classpath工具 11-16
linux环境下java开发 linux和windows比较 06-25
struts构建文件上传(四) 11-17
程序分享:从uddi注册中心查询服务 11-17
使用sets 11-17
编写 jscript 代码 11-16
java与javascript的通信lixiaolong3456 11-17
java教师:给java初学者 11-16
struts源码研究-bean-message标签篇 11-17
jboss技术支持文档(一) 11-17
说文解字:解读java中的“垃圾方言” 11-17
j2ee 笔记 --基础 11-17
保护applet免遭窃取的小技 11-17
java设计模式---prototype(原型)模式 11-17
j2me学习系列之如何将j2me与xml集成 11-17
用java做时间的减法 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息