⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 j-javalibrary-5-2.shtml

📁 构造java库
💻 SHTML
字号:
<html><!--This panel generated from the developerWorks XML tag set for tutorials, Version 1.8.9.  (C) Copyright 2000 IBM Corporation.  All rights reserved.--><head><title>Design issue: Debugging information</title><style type="text/css"><!--      body                 { color: #000000; background-color: #ffffff; }       dt                   { font-weight: bold; }       code                 { font-family: Courier, monospace; font-size: +2; }      titleCode            { font-family: Courier, monospace; font-size: +4; }      ol.alpha             { list-style-type: upper-alpha; }      span.center          { text-align: center; }      span.right           { text-align: right; }      span.constant        { color: #990099; }      span.keyword         { color: #993333; }      span.function-name   { color: #666633; }      span.comment         { color: #666666; }      span.string          { color: #0000ff; }      span.variable-name   { color: #000066; }      span.type            { color: #006600; }      span.spacer          { font-size: 1pt; }    --></style><meta http-equiv="content-Type" content="text/html; charset=gb2312"></head><body bgcolor="ffffff"><script language="javascript"><!--var emailAbstract="This tutorial reviews how the Java language can help you make a good, reusable library.  It covers the key principles of good library design and the most efficient ways to implement each of these ideas in the Java language.";var justTitle="Build your own Java library";var tutorialPrereqs="http://www-4.ibm.com/software/developer/education/r-jlib.shtml";var menu1blurb="Tutorial tips"; var menu2blurb="Introduction to libraries"; var menu3blurb="Design issue: Encapsulation"; var menu4blurb="Design issue: Extensibility"; var menu5blurb="Design issue: Debugging information"; var menu6blurb="The source code"; var menu7blurb="Wrapup";       var browser = "x";      if (navigator.userAgent.indexOf("Mozilla/4") != -1) browser = "N3";      else if (navigator.userAgent.indexOf("Mozilla/3") != -1) browser = "N3";      else browser = "x";            if (browser=="N3")      {    var menu1over=new Image(108,68); var menu1out=new Image(108,68); var menu2over=new Image(108,68); var menu2out=new Image(108,68); var menu3over=new Image(108,68); var menu3out=new Image(108,68); var menu4over=new Image(108,68); var menu4out=new Image(108,68); var menu5over=new Image(108,68); var menu5out=new Image(108,68); var menu6over=new Image(108,68); var menu6out=new Image(108,68); var menu7over=new Image(108,68); var menu7out=new Image(108,68);         var topmainover=new Image(77,15);        var topmainout=new Image(77,15);        var bottommainover=new Image(77,15);        var bottommainout=new Image(77,15);        var topsectionover=new Image(98,15);        var topsectionout=new Image(98,15);        var bottomsectionover=new Image(98,15);        var bottomsectionout=new Image(98,15);        var topfeedbackover=new Image(80,15);        var topfeedbackout=new Image(80,15);        var bottomfeedbackover=new Image(80,15);        var bottomfeedbackout=new Image(80,15);        var toppreviousover=new Image(77,15);        var toppreviousout=new Image(77,15);        var bottompreviousover=new Image(77,15);        var bottompreviousout=new Image(77,15);        var topnextover=new Image(60,15);        var topnextout=new Image(60,15);        var bottomnextover=new Image(60,15);        var bottomnextout=new Image(60,15);        var topnextsectionover=new Image(108,15);        var topnextsectionout=new Image(108,15);        var bottomnextsectionover=new Image(108,15);        var bottomnextsectionout=new Image(108,15);          menu1over.src="imagemaster/himenu1.jpg"; menu1out.src="imagemaster/menu1.jpg"; menu2over.src="imagemaster/himenu2.jpg"; menu2out.src="imagemaster/menu2.jpg"; menu3over.src="imagemaster/himenu3.jpg"; menu3out.src="imagemaster/menu3.jpg"; menu4over.src="imagemaster/himenu4.jpg"; menu4out.src="imagemaster/menu4.jpg"; menu5over.src="imagemaster/himenu5.jpg"; menu5out.src="imagemaster/menu5.jpg"; menu6over.src="imagemaster/himenu6.jpg"; menu6out.src="imagemaster/menu6.jpg"; menu7over.src="imagemaster/himenu7.jpg"; menu7out.src="imagemaster/menu7.jpg";         var mainblurb="Main menu";        var sectionblurb="Section menu";        var feedbackblurb="Give feedback on this tutorial";        var previousblurb="Go to previous panel";        var nextblurb="Go to next panel";        var nextsectionblurb="Go to next section";        topmainover.src="../i/h-main.gif";        topmainout.src="../i/main.gif";        bottommainover.src="../i/h-main.gif";        bottommainout.src="../i/main.gif";        topsectionover.src="../i/h-section.gif";        topsectionout.src="../i/section.gif";              bottomsectionover.src="../i/h-section.gif";        bottomsectionout.src="../i/section.gif";              topfeedbackover.src="../i/h-feedback.gif";        topfeedbackout.src="../i/feedback.gif";        bottomfeedbackover.src="../i/h-feedback.gif";        bottomfeedbackout.src="../i/feedback.gif";        toppreviousover.src="../i/h-previous.gif";        toppreviousout.src="../i/previous.gif";        bottompreviousover.src="../i/h-previous.gif";        bottompreviousout.src="../i/previous.gif";        topnextover.src="../i/h-next.gif";        topnextout.src="../i/next.gif";        bottomnextover.src="../i/h-next.gif";        bottomnextout.src="../i/next.gif";        topnextsectionout.src="../i/nextsection.gif";        topnextsectionover.src="../i/h-nextsection.gif";        bottomnextsectionout.src="../i/nextsection.gif";        bottomnextsectionover.src="../i/h-nextsection.gif";      }      function iOut(image)      {        if (browser=="N3")document[image].src=eval(image + "out.src");      }      function iOver(image)      {        if (browser=="N3")document[image].src=eval(image + "over.src");      }     // --></script><a href="#navskip"><img border="0" alt="Skip to main content" src="../i/c.gif"></a><!--#include virtual="/developerWorks/inc/tuto-inc/tuto-header.inc" --><!--#include virtual="/developerWorks/inc/tuto-inc/tuto-java-header.inc" --><br x="7"><img src="imagemaster/masthead.jpg" alt="Build your own Java library"></td><td width="*" valign="bottom" align="right"><a border="0" href="../j-library.zip"><img alt="下载教程 zip 文件" border="0" src="../i/icon-zip.gif"></a>&nbsp;&nbsp;&nbsp;&nbsp;</td></tr><tr><td height="2" colspan="2"><img height="2" width="1" src="../i/c.gif"></td></tr></table><TABLE border="0" cellpadding="0" cellspacing="0" width="100%"><TR><TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD></TR><TR><TD background="../i/sw-gold.gif"><a border="0" href="index.shtml" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="Main menu" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index5.shtml"><img alt="Section menu" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-javalibrary-7-3.shtml"><img alt="Give feedback on this tutorial" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-javalibrary-5-1.shtml"><img alt="Previous" border="0" src="../i/previous.gif" name="topprevious"></a></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xnext.gif"></TD></TR><TR><TD width="100%" colspan="5"></TD><TD width="108" height="1" bgcolor="#000000" align="right"><IMG alt="" height="1" width="108" src="../i/c.gif"></TD></TR><TR><TD colspan="5"></TD><TD align="right"><a border="0" onMouseOver="iOver('topnextsection'); iOver('bottomnextsection'); self.status=nextsectionblurb; return true;" onMouseOut="iOut('topnextsection'); iOut('bottomnextsection'); self.status=''; return true;" href="j-javalibrary-6-1.shtml"><img alt="Next Section" src="../i/nextsection.gif" border="0" name="topnextsection"></a></TD></TR></TABLE><table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%"><tr valign="bottom"><a name="navskip"></a><td height="25" colspan="4"><img border="0" height="25" width="562" src="imagemaster/titlebar5.jpg" alt="5. Design issue: Debugging information"></td></tr><tr><td bgcolor="ffffff" width="15">&nbsp;</td><td bgcolor="ffffff" width="12">&nbsp;</td><td valign="top" align="left" bgcolor="ffffff" width="*"><p><br x="7"><table cellspacing="0" cellpadding="0" border="0"><tr><td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>A little text goes a long way</b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr>    page 2 of 2</nobr></font></td></tr></table><br x="7"><br x="7"></p><font size="2" face="Verdana, Arial, Helvetica"><p>

If you supply the source with your library, a user <i>might</i> consider using a debugger, but you shouldn't count on that.

</p><p>

A better way to counter this inevitable problem is to add debugging <code style="font-family: Courier New, Courier, monospace; font-size: 12">println()</code> statements to your code to force each piece of the code to report on what it is doing.  Watching this information can often help the user know where something is going wrong.

</p><p>

The following example demonstrates this technique. The user's code can install a <code style="font-family: Courier New, Courier, monospace; font-size: 12">PrintStream</code> object by calling the static method <code style="font-family: Courier New, Courier, monospace; font-size: 12">Server.setDebugStream()</code>.  Once implemented, the debug information will be sent to the provided stream.

</p><p>

<pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
  // set this to a print stream if you want debug info
  // sent to it; otherwise, leave it null
  static private PrintStream debugStream;

  // call this to send the debugging output somewhere
  static public void setDebugStream( PrintStream ps ) {
    debugStream = ps;
  }
</code></pre>

</p><p>

You can then sprinkle your library with calls to the <code style="font-family: Courier New, Courier, monospace; font-size: 12">debug()</code> method:

</p><p>

<pre><code style="font-family: Courier New, Courier, monospace; font-size: 12">
  // send debug info to the print stream, if there is one
  static public void debug( String s ) {
    if (debugStream != null)
      debugStream.println( s );
  }
</code></pre>

</p><br x="7"></font></td></tr></table><TABLE border="0" cellpadding="0" cellspacing="0" width="100%"><TR><TD align="right" colspan="6"><a border="0" onMouseOver="iOver('topnextsection'); iOver('bottomnextsection'); self.status=nextsectionblurb; return true;" onMouseOut="iOut('topnextsection'); iOut('bottomnextsection'); self.status=''; return true;" href="j-javalibrary-6-1.shtml"><img alt="Next Section" src="../i/nextsection.gif" border="0" name="bottomnextsection"></a></TD></TR><TR><TD width="100%" colspan="5"></TD><TD width="108" height="1" bgcolor="#000000" align="right"><IMG alt="" height="1" width="108" src="../i/c.gif"></TD></TR><TR><TD background="../i/sw-gold.gif"><a border="0" href="index.shtml" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="Main menu" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index5.shtml"><img alt="Section menu" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-javalibrary-7-3.shtml"><img alt="Give feedback on this tutorial" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topprevious'); iOver('bottomprevious'); self.status=previousblurb; return true;" onMouseOut="iOut('topprevious'); iOut('bottomprevious'); self.status=''; return true;" href="j-javalibrary-5-1.shtml"><img alt="Previous" border="0" src="../i/previous.gif" name="bottomprevious"></a></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xnext.gif"></TD></TR><TR><TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD></TR></TABLE><!--#include virtual="/developerWorks/inc/tuto-inc/tuto-footer.inc" --></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -