📄 ch29.htm
字号:
<HTML><HEAD><TITLE>developer.com - Reference</TITLE><META NAME="Author" CONTENT="developer.com"><META NAME="Keywords" CONTENT="javascript, ActiveX, VRML, Push, channel,push channel, applet, directory, news, community, developer, internet,intranet, java, java applet, J++, javabeans, java products, tools, javatools, JDK, java development kit, java development environment, JIT,JavaPlan, enterprise tools, JVM, Java Virtual Machine, Java resources, API,browser, java documentation, CDF, Common Desktop Format, RDF, ResourceDescription Framework, Netcaster, Netcast, Castanet, meta-language, metadata,solaris, Solaris, SUN, CGI, Perl, Oracle, database, network, Netscape API,Apache, apache, SSL, mSQL, Netscape, Fasttrack, FastTrack,industry standard, ActiveX bridge, web, internet, site, sitedesign, microsoft, active server pages, asp, iis, nt, win95, 95, html, cgi,frontpage, business, corporate, vbscript, javascript, activex, isapi,database, access, sql, server, www, 3.0, 3.01, 3.02, 3.03, 3.04, 4.0,4.0b2, 4.01, 4.03, 4.04, connection, online, commerce, publisher,publishing, publish, webmaster, dynamic html, dhtml, windows 98, netscape,netscape software, shareware, magazines, jobs, japan, free, freeware, freestuff"><META NAME="description" CONTENT="Developer.com is the leading onlineservice for developers. It includes Gamelan, the official directory for Java, as wellas news, information, tutorials, and directories for other Internet technologiesincluding ActiveX, JavaScript, perl, VRML, Java Beans, push technologies, andother Internet and intranet technologies. Developer.com is also the home ofDeveloper Direct, the Professional Developer's Store, where developers canpurchase and download thousands of Web, authoring and other developmentproducts at some of today's lowest prices."><META NAME="distribution" CONTENT="global"><META NAME="resource-type" CONTENT="document"><META NAME="language" CONTENT="en"></HEAD><BODY BACKGROUND="/images/curve_r-wide.gif" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#DD0000" VLINK="#999999" ALINK="#FF0000"><TABLE><TR><TD VALIGN=TOP WIDTH=120><A HREF="/" ><IMG SRC="/images/masthead_r.gif" WIDTH="100" HEIGHT="78" BORDER="0"ALT="Developer.com Logo"></a></TD><TD><a target="resource window" href="http://adserver.developer.com/cgi-bin/accipiter/adclick.exe/AREA=DCBAN.REF/DT=null"><img src="http://adserver.developer.com/cgi-bin/accipiter/adserver.exe/AREA=DCBAN.REF/DT=null" HEIGHT="60" WIDTH="468" ALT="Click here to support our advertisers"><BR><CENTER>Click here to support our advertisers</CENTER></a></TD></TR></TABLE><!--outer table--><TABLE><!--outer table--><TR><!--outer table--><TD VALIGN="TOP"><!--navbar info --><table><tr><td><A HREF="http://www.developerdirect.com/" ><IMG SRC="/images/button_s.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>SHOPPING</b></font></a></td></tr><tr><td><A HREF="/community/jobs/c_jobs.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>JOB BANK</B></font></a></td></tr><tr><td><A HREF="http://www.classifieds2000.com/cgi-cls/display.exe?Developer+computer+searchcomputercategories"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>CLASSIFIEDS</B></font></a></td></tr><tr><td><A HREF="/directories/directories.html"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>DIRECTORIES</B></font></a></td></tr><tr><td><A HREF="/reference/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>REFERENCE</B></font></a><br><a href="/reference/r_library.html"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">Online Library</a></font></td></tr><tr><td><A HREF="/classroom/"><IMG SRC="/images/button_r.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>LEARNING CENTER</B></font></a></td></tr><tr><td><A HREF="/journal/"><IMG SRC="/images/button_n.gif" width="15" height="11" BORDER="0"><B><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000">JOURNAL</font></B></a></td></tr><tr><td><A HREF="/news/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>NEWS CENTRAL</b></font></a></td></tr><tr><td><A HREF="/downloads/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>DOWNLOADS</b></font></a></td></tr><tr><td><A HREF="/community/" ><IMG SRC="/images/button_c.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><B>COMMUNITY</B></font></a></td></tr><tr><td><A HREF="/calendar/"><IMG SRC="/images/button_d.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>CALENDAR</b></font></a></td></tr><tr><td><A HREF="/about/"><IMG SRC="/images/button_a.gif" width="15" height="11" BORDER="0"><FONT FACE="Arial, Helvetica" SIZE="-1" color="#000000"><b>ABOUT US</b></a></font></td></tr></table><table><tr><td><form action="/subscribe/list.cgi" method=post ><img src="/images/rule.gif" width="100" height="2"><br><B><FONT FACE="Arial, Helvetica" SIZE="-1">Journal:</b></font><br><input name='email' value='your e-mail' size="11"><input type='hidden' name='state' value='Subscribe'></td></tr><tr><td><input type='submit' value='Subscribe'><br><font size="-1">Get the weekly email highlights from the most popular journal for developers!<br><a href="http://www.developer.com/subscribe.html">Current issue</a><img src="/images/rule.gif" width="100" height="2"><input type='hidden' name='name' value='New Subscriber'></FORM></td></tr></table><!--/navbar--><font size="-1"><A HREF="http://www.developer.com">developer.com</a><BR><A HREF="http://www.developerdirect.com">developerdirect.com</a><BR><A HREF="http://www.htmlgoodies.com">htmlgoodies.com</a><BR><A HREF="http://www.javagoodies.com">javagoodies.com</a><br><A HREF="http://www.jars.com">jars.com</a><BR><a href="http://www.intranetjournal.com">intranetjournal.com</a><br><a href="http://www.javascripts.com">javascripts.com</a><br></font><!--outer table--></TD><!--outer table--><TD VALIGN="TOP"><BLOCKQUOTE><P><img src="/images/header_r.gif" width="440" height="50"><P><MAP NAME ="header_r2.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/author.html" COORDS ="361,0,440,29"><AREA SHAPE ="RECT" HREF ="/reference/basic.html" COORDS ="254,0,360,29"><AREA SHAPE ="RECT" HREF ="/reference/help.html" COORDS ="183,0,251,29"><AREA SHAPE ="RECT" HREF ="/reference/cool.html" COORDS ="100,0,181,29"><AREA SHAPE ="RECT" HREF ="/reference/mybook.html" COORDS ="0,0,100,29"></MAP><MAP NAME ="header_r1.gif.map"><AREA SHAPE ="RECT" HREF ="/reference/personalize.html" COORDS ="339,0,439,29"></MAP><p><font face="Arial,Helvetica" size="-1" color="#006666"><A HREF="/reference/r_library.html"><B>All Categories</A> :</B><b><A HREF="/reference/r_java.html">Java</A></b></font><p><META><H1><FONT SIZE=6 COLOR=#Ff0000>Chapter 29</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Packages and Interfaces</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#Packages">Packages</A><UL><LI><A HREF="#CreatingYourOwnPackages">Creating Your Own Packages</A><LI><A HREF="#ExampleCreatingaSimplePackage">Example: Creating a Simple Package</A><LI><A HREF="#ExampleUsingtheNewPackage">Example: Using the New Package</A><LI><A HREF="#ExampleExtendingthePackage">Example: Extending the Package</A></UL><LI><A HREF="#Interfaces">Interfaces</A><UL><LI><A HREF="#TheBasicInterface">The Basic Interface</A><LI><A HREF="#ExampleCreatinganInterface">Example: Creating an Interface</A><LI><A HREF="#ImplementinganInterface">Implementing an Interface</A></UL><LI><A HREF="#Summary">Summary</A><LI><A HREF="#ReviewQuestions">Review Questions</A><LI><A HREF="#ReviewExercises">Review Exercises</A></UL><HR><P>As you write more and more Java source code, you're going to starthaving a hard time finding the snippets of code you need for yourcurrent project. You may, for example, have a number of classesthat are related in some way, yet are scattered in separate filesmaking it difficult to determine exactly what you have. One solutionto this problem is packages, which enable you to organize yourclasses into logical groups.<P>You may also have a number of classes that contain capabilitiesthat you'd like to use in a newly derived class. However, Javadoes not allow multiple inheritance, which is deriving a singleclass directly from two or more classes simultaneously. To getaround this deliberate limitation, Java uses something calledinterfaces, which enable the programmer to define capabilitiesfor classes without implementing those capabilities until theclasses that use the interface are defined. In this chapter, youstudy both packages and interfaces.<H2><A NAME="Packages"><FONT SIZE=5 COLOR=#Ff0000>Packages</FONT></A></H2><P>You may not realize it, but you've been using Java packages sincethe first applet you created. That's because all of the Java Developer'sKit's classes are organized into packages. You've been importingthose packages into your source code with code similar to this:<BLOCKQUOTE><PRE>import java.awt.*;import java.applet.*;</PRE></BLOCKQUOTE><P>If you examine either of these lines, you'll see that each startswith the word <TT>java</TT> followed by a package name and anasterisk, each element being separated by a dot. The use of thedifferent names separated by the dots illustrates the hierarchythat Java's creators used when they created the Java packages.This hierarchy is used not only as a way of referring to classnames in source code, but also as a way to organize the .CLASSfiles that comprise a class library.<P>If you look in your JAVA\LIB folder, you'll find the JAVA folder,within which is the AWT and APPLET folders. In the AWT and APPLETfolders are the .class files for the <TT>awt</TT> and <TT>applet</TT>packages. You can see that the <TT>import</TT> lines indicatethe directory structure under which the package source-code filesare stored. When Java's compiler runs into such import lines,it expects the directory structure to match the package's hierarchy.<P>In the preceding two sample <TT>import</TT> lines, the asterisksmean that Java should import all of the classes of the <TT>awt</TT>and <TT>applet</TT> packages into the applet you're writing. Ifyou wanted to, you could streamline the import process by importingexactly the classes used in your source code. (When doing this,keep in mind that Java is case sensitive.) You would do this byreplacing the asterisk with the name of the class you want toimport. For example, to import only the <TT>Button</TT> classfrom the <TT>awt</TT> package, you use this line:<BLOCKQUOTE><PRE>import java.awt.Button;</PRE></BLOCKQUOTE><P>However, because you frequently need to access more than a singleclass in a package, it's often more convenient to import all ofthe classes at once.<P><CENTER><TABLE BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD><B>NOTE</B></TD></TR><TR VALIGN=TOP><TD><BLOCKQUOTE>If you like, you can do without <TT>import</TT> statements at all. To do this, you need to use fully qualified package names when referring to a Java class. For example, to create a new button object, you could write <TT>java.awt.Button button = new java.awt.Button(label)</TT>. As you can see, however, such program lines become unwieldy, which is why Java supports the <TT>import</TT> statement.</BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H3><A NAME="CreatingYourOwnPackages">Creating Your Own Packages</A></H3><P>As you write your own classes, you're going to want to organizethe related classes into packages just as Java does. You do thisby organizing your classes into the same sort of hierarchy. Forexample, you may want to start with a folder called MYPACKAGESinto which you'll store the .class files that make up your classlibraries. This folder should be in the folder in which your mainsource-code files are located. If you've been following the instructionsgiven earlier in this book, you've been using a folder calledCLASSES for this purpose.<P>To add a class to a package, you put the following line at thetop of the class's source code:<BLOCKQUOTE><PRE>package PackageName;</PRE></BLOCKQUOTE><P>Here, the keyword <TT>package</TT> tells Java that you want toadd this class to a package. The name of this package will be
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -