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

📄 ch4.htm

📁 Java_by_Example,初级经典例子哦,珍藏版本
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<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&nbsp;4</FONT></H1><H1><FONT SIZE=6 COLOR=#Ff0000>Object-Oriented Programming Concepts</FONT></H1><HR><P><CENTER><B><FONT SIZE=5><A NAME="CONTENTS">CONTENTS</A></FONT></B></CENTER><UL><LI><A HREF="#FromSwitchestoObjects">From Switches to Objects</A><LI><A HREF="#AnObviousYetBrilliantSolution">An Obvious, Yet Brilliant, Solution</A><LI><A HREF="#ObjectOrientedProgramming">Object-Oriented Programming</A><UL><LI><A HREF="#Encapsulation">Encapsulation</A><LI><A HREF="#ClassesasDataTypes">Classes as Data Types</A><LI><A HREF="#Inheritance">Inheritance</A><LI><A HREF="#Polymorphism">Polymorphism</A><LI><A HREF="#ExampleEncapsulationInheritanceandPolymorphism">Example: Encapsulation, Inheritance, and Polymorphism</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>Programming languages, like their spoken-language kin, evolveover time. They are constantly refined and focused to meet theever-changing needs of their users. Like other modern programminglanguages such as C++, Java is an amalgamation of all the techniquesdeveloped over the years. Therefore, we'll start exploring object-orientedprogramming (OOP) by briefly looking at the history of programminglanguages. Knowing where object-oriented ideas came from willhelp you to better understand why they are an important part ofmodern programming languages. Once you understand why OOP wasdeveloped, you'll learn exactly what makes a programming languageobject-oriented.<H2><A NAME="FromSwitchestoObjects"><FONT SIZE=5 COLOR=#Ff0000>From Switches to Objects</FONT></A></H2><P>Back in the dark ages of computing, technicians programmed computersby flipping banks of switches, with each switch representing asingle bit of information. In those days, even the simple programsrequired agonizing patience and precision. As the need for moresophisticated programs grew, so did the need for better ways towrite these programs. The need to make computer programming quickerand simpler spurred the invention of assembly language and, shortlythereafter, high-level languages such as FORTRAN.<P>High-level languages enable programmers to use English-like commandsin their programs and to be less concerned with the details ofprogramming a computer and more concerned with the tasks thatneed to be completed. For example, in assembly language-a low-levellanguage-it might take several instructions to display a lineof text on the screen. In a high-level language, there's usuallya single command such as <TT>PRINT </TT>that accomplishes thistask.<P>With the advent of high-level languages, programming became accessibleto more people; writing program code was no longer exclusivelythe domain of specially trained scientists. As a result, computingwas used in increasingly complex roles. It was soon clear, however,that a more efficient way of programming was needed, one thatwould eliminate the obscure and complex &quot;spaghetti code&quot;that the early languages produced.<P>Programmers needed a new way of using high-level languages, onethat enabled them to partition their programs into logical sectionsthat represented the general tasks to be completed. Thus, thestructured-programming paradigm was born. Structured programmingencourages a top-down approach to programming, in which the programmerfocuses on the general functions that a program must accomplishrather than the details of how those functions are implemented.When programmers think and program in top-down fashion, they canmore easily handle large projects without producing tangled code.<P>For an analogy, consider an everyday task such as cleaning a house.If you wanted to write out the steps needed to complete this task,you'd write something like this:<P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>Go to the living room.<BR>Dust the coffee table.<BR>Dust the end tables.<BR>Vacuum the rug.<BR>Go to the Kitchen.<BR>Wash the dishes.<BR>Wipe the counters.<BR>Clean the stove.<BR>Wipe off the refrigerator.<BR>Sweep the floor.<BR>Go to the bedroom.<BR>Make the bed.<BR>Dust the bureau.<BR>Vacuum the rug.</BLOCKQUOTE><P>The preceding list of steps is similar, in theory, to how you'dprogram a computer without using a top-down approach. Using thetop-down programming approach, you'd revise the &quot;program&quot;as follows:<P><IMG ALIGN=RIGHT SRC="pseudo.gif" HEIGHT=94 WIDTH=94 BORDER=1><BLOCKQUOTE>TOP LEVEL<BR>&nbsp;&nbsp;&nbsp;&nbsp;Clean the Living Room.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Clean the Kitchen.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Clean the Bedroom.</BLOCKQUOTE><BLOCKQUOTE>SECOND LEVEL<BR>Clean the Living Room<BR>START<BR>&nbsp;&nbsp;&nbsp;&nbsp;Go to the living room.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Dust the coffee table.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Dust the end tables.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Vacuum the rug.<BR>END</BLOCKQUOTE><BLOCKQUOTE>Clean the Kitchen<BR>START<BR>&nbsp;&nbsp;&nbsp;&nbsp;Go to the Kitchen.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Wash the dishes.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Wipe the counters.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Clean the stove.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Wipe off the refrigerator.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Sweep the floor.<BR>END</BLOCKQUOTE><BLOCKQUOTE>Clean the Bedroom<BR>START<BR>&nbsp;&nbsp;&nbsp;&nbsp;Go to the bedroom.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Make the bed.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Dust the bureau.<BR>&nbsp;&nbsp;&nbsp;&nbsp;Vacuum the rug.<BR>END</BLOCKQUOTE><P>Now, if you're only interested in seeing what the &quot;program&quot;does, you can glance at the top level and see that these are instructionsfor cleaning the living room, kitchen, and bedroom. If that'sall you need to know, you need to look no further. If, however,you want to know exactly how to clean the living room, you cango down one level in the top-down structure and find the detailedinstructions for cleaning the living room. Yes, the top-down approachtends to make programs longer, but it also adds clarity to theprogram, because you can hide the details until you really needthem.<P>Today, the need for efficient programming methods is more importantthan ever. The size of the average computer program has growndramatically and now consists of hundreds of thousands of codelines. (It's rumored that Windows 95 comprises as much as 15 millionlines of code. It boggles the mind!) With these huge programs,reusability is critical. Again, a better way of programming isneeded-and that better way is object-oriented programming.<H2><A NAME="AnObviousYetBrilliantSolution"><FONT SIZE=5 COLOR=#Ff0000>An Obvious, Yet Brilliant, Solution</FONT></A></H2><P>The world consists of many objects, most of which manipulate otherobjects or data. For example, a car is an object that manipulatesits speed and direction to transport people to a different location.This car object encapsulates all the functions and data that itneeds to get its job done. It has a switch to turn it on, a wheelto control its direction, and brakes to slow it down. These functionsdirectly manipulate the car's data, including direction, position,and speed.<P>When you travel in a car, however, you don't have to know thedetails of how these operations work. To stop a car, for example,you simply step on the brake pedal. You don't have to know howthe pedal stops the car. You simply know that it works.<P>All these functions and data work together to define the object

⌨️ 快捷键说明

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