国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

在Eclipse 3.1體驗J2SE 5.0新特性五(圖)

2019-11-18 13:01:25
字體:
來源:轉載
供稿:網友

  清單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)的應用范圍其實是很廣泛的。在我們的應用中充分的利用元數據,可以提高的軟件的質量和可維護性。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 饶阳县| 潼关县| 广丰县| 定南县| 沽源县| 进贤县| 花垣县| 威宁| 通海县| 洛川县| 巫山县| 崇阳县| 政和县| 安吉县| 齐齐哈尔市| 玛多县| 澄迈县| 洪湖市| 依兰县| 合阳县| 滦南县| 泰兴市| 济阳县| 平昌县| 平陆县| 治县。| 荔浦县| 德钦县| 仁寿县| 个旧市| 马边| 台山市| 方城县| 洪江市| 青龙| 改则县| 牙克石市| 温宿县| 西吉县| 林甸县| 江北区|