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

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

細(xì)細(xì)講述Java技術(shù)開發(fā)的規(guī)則之開發(fā)篇

2019-11-18 12:01:16
字體:
供稿:網(wǎng)友

  本文介紹的java規(guī)則的說明分為3個主要級別,本篇拋棄了平時開發(fā)中很少碰到的情況,那些用得比較少的以后再高級篇里面出現(xiàn)。并有六個有用的國際軟件開發(fā)重要注重的有關(guān)String的問題,遵守了這些規(guī)則可以提高程序的效率、使代碼又更好的可讀性等。
  
  (1)假如有JDBC連接沒有關(guān)掉的話,需要在"finally"方法中關(guān)掉
  
  假如數(shù)據(jù)庫連接失敗或者是沒有釋放連接,看上去無關(guān)緊要。但是其他的用戶就需要用更長的時間等待連接,這樣數(shù)據(jù)庫利用效率就會下降。確保你的代碼在任何情況下,包括出錯或者程序異常終止的情況下都釋放數(shù)據(jù)庫連接。在"finally"方法中關(guān)掉連接,就可以確保這一點(diǎn)。
  
  錯誤示例:
  
  try {
  Statement stmt = con.createStatement();
  } catch(SQLException e)
  {
  e.PRintStackTrace();
  }
  
  正確示例:
  
  try {
  Statement stmt = con.createStatement();
  } finally
  {
  if (con != null && !con.isClosed())
  {
  con.close();
  }
  }
  
  (2)盡量避免使用'Thread.resume ()', 'Thread.stop ()', 'Thread.suspend ()'和 'Runtime.runFinalizersOnExit ()' 方法。
  
  這些方法在平時的開發(fā)或者是教科書里面也有用到過,但是這些方法會導(dǎo)致四鎖的傾向。一下有充足的資料來說明為什么不建議用上述方法。
  
  (3)在表示長整常量的時候,用L來代替l,因?yàn)閘很輕易和1混一起。
  
  錯誤示例:
  
  long temp = 23434l;
  
  正確示例:
  
  long temp = 23434L;
  
  (4)最好在jsp開頭寫一條注釋
  
  在jsp文件頭上面寫一條注釋,這樣可以幫助別人來理解你的代碼。這條規(guī)則不僅適用于jsp,更是用于任何開發(fā)的文檔。
  
  (5)明確的初始化一個構(gòu)造類里面的所有的字段
  
  因?yàn)闆]有初始化的字段會是一個潛在的bug,所以最好初始化類里面的所有的字段。非凡是靜態(tài)的字段,最好在一開始就分配一個初始值
  
  錯誤示例:
  
  public class CSI {
  public CSI () {
  this (12);
  k = 0;
  }
  
  public CSI (int val) {
  j = val;
  }
  
  private int i = 5;
  private int j;
  private int k;
  }
  
  正確示例:
  
  public class CSIFixed {
  public CSIFixed () {
  this (12);
  }
  
  public CSIFixed (int val) {
  j = val;
  k = 0;
  }
  
  private int i = 5;
  private int j;
  private int k;
  }
  
  (6)國際化開發(fā)建議:邏輯操作符不要再一個單個的字符的前面或者后面
  
  一個單個字符的前后不要用邏輯操作符,假如代碼要在一個國家環(huán)境中運(yùn)行的話。我們可以使用字符比較方法,這些方法使用統(tǒng)一字符比較標(biāo)準(zhǔn)來定義字符的屬性的。
  
  錯誤示例:
  
  public class CLO
  {
  public boolean isLetter (char ch)
  {
  boolean _isLetter =
  ( ch >= 'a' && ch <= 'z')
  //錯誤
   (ch >= 'A' && ch <= 'Z');
  return _isLetter;
  }
  }
  
  正確示例:
  
  public class CLOFixed
  {
  public boolean isLetter (char ch)
  {
  boolean _isLetter =
  Character.isLetter(ch);
  return _isLetter;
  }
  }
  
  (7)國際化開發(fā)建議:不要對日期對象使用'Date.toString ()'
  
  不要使用'Date.toString()'方法,日期格式對于地區(qū)和語言不同的國家來說是不一樣的,務(wù)必不要使用。
  
  錯誤示例:'DateFormat'類提供了一個預(yù)定義的格式類型來指定本地的格式。
  
  public void printToday ()
  {
  Date today = new Date ();
  String todayStr = today.toString ();
  System.out.println (todayStr);
  }
  
  正確示例:
  
  public void printToday ()
  {
  Locale currentLocale =
  Locale.getDefault ();
  DateFormat dateFormatter =
  DateFormat.getDateInstance (
  DateFormat.DEFAULT,
  currentLocale);
  Date today = new Date ();
  String todayStr =
  dateFormatter.format (today);
  System.out.println (todayStr);
  }
  
  (8)國際化開發(fā)建議:不要對數(shù)字變量使用'toString ()'方法
  
  在全球化的開發(fā)中,不要對數(shù)字變量使用'toString()'方法,對于java.lang.Number的任何子類都適用。包括:BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, and Short.對于這樣的情況,java里也與定義了"NumberFormat"方法來格式化。
  
  錯誤示例:
  
  public class NTS {
  public void method (Double amount)
  {
  String amountStr = amount.toString ();
  System.out.println (amountStr);
  }
  }
  
  正確示例:
  
  public class NTSFixed
  {
  public void method (Double amount)
  {
  Locale currentLocale =
  Locale.getDefault ();
  NumberFormat numberFormatter =
  NumberFormat.getNumberInstance
  (currentLocale);
  String amountStr =
  numberFormatter.format (amount);
  //
  System.out.println (amountStr + '
  ' + currentLocale.toString ());
  }
  }
  
  (9)國際化開發(fā)建議:不要使用'String.equals ()'方法
  
  建議不要使用'String.equals()'方法,因?yàn)樵诮y(tǒng)一字符比較標(biāo)準(zhǔn)中不一定按照相關(guān)的順序來比較。'Collator'提供的預(yù)定義整理規(guī)則來排序string,Collator類調(diào)用'getInstance()'方法,一般來說,可以為默認(rèn)的本地創(chuàng)建一個Collator。
  
  例如:Collator myCollator = Collator.getInstance();創(chuàng)建Collator的時候你也可以指定一個非凡的locale。例如:Collator myFrenchCollator = Collator.getInstance(Locale.FRENCH);然后就可以調(diào)用'Collator.compare ()'來執(zhí)行一個本地的字符比較myCollator.compare(s1,s2);從這里可以了解更多的有關(guān)Collator類的信息:http://java.sun.com/docs/books/tutorial/i18n/text/collationintro.Html
  
  錯誤示例:
  
  public class SE
  {
  public boolean compstr
  (String s1, String s2) {
  boolean b = (s1.equals (s2));
  return b;
  }
  }
  
  正確示例:
  
  public class SEFixed {
  public boolean compstr (String s1, String s2)
  {
  Collator myCollator =
  Collator.getInstance ();
  boolean b =
  (myCollator.compare(s1,s2) == 0);
  return b;
  }
  }
  
  (10)國際化開發(fā)建議:不要使用'StringTokenizer()'方法
  
  錯誤示例:
  
  StringTokenizer st = new StringTokenizer(str);
  
  (11)國際化開發(fā)建議:不要使用'Time.toString ()'方法
  
  因?yàn)闀r間的格式各個國家也不一樣。假如你使用日期格式類,你的應(yīng)用就能夠在世界上各個地方正確的顯示時間和日期了。首先,用'getTimeInstance()'方法創(chuàng)建一個formatter。然后,調(diào)用'format ()'方法。
  
  錯誤示例:
  
  public class TTS {
  public void printTime (Time t1) {
  String timeStr = t1.toString ();
  System.out.println (timeStr);
  }
  }
  
  正確示例:
  
  import java.sql.Time;
  import java.text.DateFormat;
  import java.util.Locale;
  
  public class TTSFixed
  {
  public void printTime (Time t1)
  {
  DateFormat timeFormatter =
  DateFormat.getTimeInstance(
  DateFormat.DEFAULT,
  Locale.getDefault ());
  String timeStr = timeFormatter.format(t1);
  System.out.println (timeStr);
  }
  }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 措勤县| 建平县| 福州市| 德安县| 双辽市| 通州区| 福泉市| 阿图什市| 西华县| 泰安市| 万安县| 佛教| 三穗县| 屏山县| 青海省| 电白县| 阿克苏市| 方城县| 邛崃市| 宝丰县| 栖霞市| 应城市| 金堂县| 铁岭市| 连山| 重庆市| 三门峡市| 伊宁县| 定陶县| 克东县| 青铜峡市| 安康市| 柳河县| 堆龙德庆县| 马鞍山市| 常山县| 竹北市| 仁寿县| 洞口县| 桂平市| 城市|