📄 class.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html><head><title>What Is a Class?</title><script language="JavaScript"><!-- hidefunction openWin(term) { url="../../information/glossary.html#" + term; myWin= window.open(url, "Glossary", "width=400,height=150,scrollbars=yes,status=no,toolbar=no,menubar=no"); myWin.focus();}//--></script><STYLE type="text/css"><!--.FigureCaption { margin-left: 1in; margin-right: 1in; font-family: Arial, Helvetica, sans-serif; font-size: smaller; text-align: justify }--></STYLE> </head><body BGCOLOR="#ffffff" LINK="#000099"><B><FONT SIZE="-1">The Java</font><sup><font size="-2">TM</font></sup> <font size="-1">Tutorial</FONT></B><br><table width="550" summary="layout"><tr><td align="left" valign="middle"><a href="message.html" target="_top"><img src="../../images/PreviousArrow.gif" width="26" height="26" align="middle" border="0" alt="Previous Page"></a><a href="../TOC.html#concepts" target="_top"><img src="../../images/TOCIcon.gif" width="26" height="26" align="middle" border="0" alt="Lesson Contents"></a><a href="inheritance.html" target="_top"><img src="../../images/NextArrow.gif" width="26" height="26" align="middle" border="0" alt="Next Page"></a></td><td align="center" valign="middle"><font size="-1"><a href="../../index.html" target="_top">Start of Tutorial</a>><a href="../index.html" target="_top">Start of Trail</a>><a href="index.html" target="_top">Start of Lesson</a></font></td><td align="right" valign="middle"><font size="-1"><a href="../../search.html" target="_top">Search</a><br><a href="http://developer.sun.com/contact/tutorial_feedback.jsp">Feedback Form</a></font></td></tr></table><img src="../../images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" ALT=""><br><font size="-1"><b>Trail</b>: Learning the Java Language<br><b>Lesson</b>: Object-Oriented Programming Concepts</font><h2>What Is a Class?</h2><blockquote>In the real world, you often have many objects of the same kind. For example, your bicycle is just one of many bicycles in the world. Using object-oriented terminology, we say that your bicycle object is an<a href="javascript:var meth=openWin; meth('instance');" onMouseOver="self.status='Look up instance in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>instance</em></font></a><a href="javascript:var meth=openWin; meth('instance');" onMouseOver="self.status='Look up instance in glossary'; return true;" onMouseOut="self.status=''; return true;"><img src="../../images/glossaryIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the glossary)"></a> of the class of objects known as bicycles. Bicycles have some state (current gear, current cadence, two wheels) and behavior (change gears, brake) in common. However, each bicycle's state is independent of and can be different from that of other bicycles.<p>When building them, manufacturers take advantage of the fact that bicycles share characteristics, building many bicycles from the same blueprint. It would be very inefficient to produce a new blueprint for every bicycle manufactured.<p>In object-oriented software, it's also possible to have many objects of the same kind that share characteristics: rectangles, employee records, video clips, and so on. Like bicycle manufacturers, you can take advantage of the fact that objects of the same kind are similar and you can create a blueprint for those objects. A software blueprint for objects is called a<a href="javascript:var meth=openWin; meth('class');" onMouseOver="self.status='Look up class in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>class</em></font></a><a href="javascript:var meth=openWin; meth('class');" onMouseOver="self.status='Look up class in glossary'; return true;" onMouseOut="self.status=''; return true;"><img src="../../images/glossaryIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the glossary)"></a> (see<span id="figure:concepts-class.gif">the following figure</span>).<center><p><IMG SRC="../../figures/java/concepts-class.gif" WIDTH="356" HEIGHT="179" ALIGN="BOTTOM" ALT="A visual representation of a class."></p><p class="FigureCaption">A visual representation of a class.</p></center><blockquote><hr><strong>Definition:</strong> A class is a blueprint that defines the variables and the methods common to all objects of a certain kind. <hr></blockquote>The class for our bicycle example would declare the instance variables necessary to contain the current gear, the current cadence, and so onfor each bicycle object. The class would also declare and provide implementations for the instance methods that allow the rider to change gears, brake, and change the pedaling cadence, as shown in<span id="figure:concepts-bicycleClass.gif">the next figure</span>.<center><p><IMG SRC="../../figures/java/concepts-bicycleClass.gif" WIDTH="317" HEIGHT="179" ALIGN="BOTTOM" ALT="The bicycle class."></p><p class="FigureCaption">The bicycle class.</p></center>After you've created the bicycle class, you can create any number of bicycle objects from that class. When you create an instance of a class, the system allocates enough memory for the object and all its instance variables. Each instance gets its own copy of all the instance variables defined in the class, as <span id="figure:concepts-2BicycleObjects.gif">the next figure</span> shows.<center><p><IMG SRC="../../figures/java/concepts-2BicycleObjects.gif" WIDTH="317" HEIGHT="427" ALIGN="BOTTOM" ALT="MyBike and YourBike are two different instances of the Bike class."></p><p class="FigureCaption"><code>MyBike</code> and <code>YourBike</code> are two different instances of the <code>Bike</code> class. Each instance has its own copy of the instance variables defined in the <code>Bike</code> class but has different values for these variables.</p></center><p>In addition to instance variables, classes can define class variables. A<a href="javascript:var meth=openWin; meth('class variable');" onMouseOver="self.status='Look up class variable in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>class variable</em></font></a><a href="javascript:var meth=openWin; meth('class variable');" onMouseOver="self.status='Look up class variable in glossary'; return true;" onMouseOut="self.status=''; return true;"><img src="../../images/glossaryIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the glossary)"></a> contains information that is shared by all instances of the class. For example, suppose that all bicycles had the same number of gears. In this case, defining an instance variable to hold the number of gears is inefficient; each instance would have its own copy of the variable, but the value would be the same for every instance. In such situations, you can define a class variable that contains the number of gears (see<span id="figure:concepts-yourBike.gif">the following figure</span>); all instances share this variable. If one object changes thevariable, it changes for all other objects of that type.</p><center><p><IMG SRC="../../figures/java/concepts-yourBike.gif" WIDTH="335" HEIGHT="492" ALIGN="BOTTOM" ALT="YourBike, an instance of Bike, has access to the numberOfGears variable in the Bike class; however, the YourBike instance does not have a copy of this class variable. "></p><p class="FigureCaption"><code>YourBike</code>, an instance of <code>Bike</code>, has access to the <code>numberOfGears</code> variable in the <code>Bike</code> class; however, the <code>YourBike</code> instance does not have a copy of this class variable. </p></center><p>A class can also declare <a href="javascript:var meth=openWin; meth('class method');" onMouseOver="self.status='Look up class methods in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>class methods</em></font></a><a href="javascript:var meth=openWin; meth('class method');" onMouseOver="self.status='Look up class methods in glossary'; return true;" onMouseOut="self.status=''; return true;"><img src="../../images/glossaryIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the glossary)"></a>. You can invoke a class method directly from the class, whereas you must invoke instance methods on a particular instance.</p><P>The <a target="_top" href="../javaOO/classvars.html">Understanding Instance and Class Members</a><a target="_top" href="../javaOO/classvars.html"><img src="../../images/tutorialIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the Learning the Java Language trail)"></a> section discusses instance variables and methods and class variables and methodsin detail.<p>Objects provide the benefit of modularity andinformation-hiding. Classes provide the benefit of reusability. Bicycle manufacturers use the same blueprint over and over again to build lots of bicycles. Software programmers use the same class, and thus the same code, over and over again to create many objects.</blockquote><h3>Objects versus Classes</h3><blockquote>You've probably noticed that the illustrations of objects and classes look very similar. And indeed, the difference between classes and objects is often the source of some confusion. In the real world, it's obvious that classes are not themselves the objects they describe; that is, a blueprint of a bicycle is not a bicycle. However, it's a little more difficult to differentiate classes and objects in software. This is partially because software objects are merely electronic models of real-world objects or abstract concepts in the first place. But it's also because the term <em>object</em> is sometimes used to refer to both classes and instances.<P>In illustrationssuch as the top part of <span id="figure:concepts-yourBike.gif">the preceding figure</span>, the class is not shaded because it represents a blueprint of an object rather than the object itself. In comparison, an object is shaded, indicating that the object exists and that you can use it.</blockquote></blockquote><img src="../../images/blueline.gif" width="550" height="8" ALIGN="BOTTOM" ALT=""><br><table width="550" summary="layout"><tr><td align="left" valign="middle"><a href="message.html" target="_top"><img src="../../images/PreviousArrow.gif" width="26" height="26" align="middle" border="0" alt="Previous Page"></a><a href="../TOC.html#concepts" target="_top"><img src="../../images/TOCIcon.gif" width="26" height="26" align="middle" border="0" alt="Lesson Contents"></a><a href="inheritance.html" target="_top"><img src="../../images/NextArrow.gif" width="26" height="26" align="middle" border="0" alt="Next Page"></a></td><td align="center" valign="middle"><font size="-1"><a href="../../index.html" target="_top">Start of Tutorial</a>><a href="../index.html" target="_top">Start of Trail</a>><a href="index.html" target="_top">Start of Lesson</a></font></td><td align="right" valign="middle"><font size="-1"><a href="../../search.html" target="_top">Search</a><br><a href="http://developer.sun.com/contact/tutorial_feedback.jsp">Feedback Form</a></font></td></tr></table><p><font size="-1"><a href="../../information/copyright.html">Copyright</a>1995-2005 Sun Microsystems, Inc. All rights reserved.</font></p></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -