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

📄 inheritance.html

📁 一些JAVA的小程序
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><html><head><title>What Is Inheritance?</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="class.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="interface.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>&gt;<a href="../index.html" target="_top">Start of Trail</a>&gt;<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 Inheritance?</h2><blockquote>Generally speaking, objects are defined in terms of classes. You know a lot about an object by knowing its class. Even if you don't know what a penny-farthing is, if I told you it was a bicycle, you would know that it had two wheels, handlebars, and pedals.<p>Object-oriented systems take this a step further and allow classes to be defined in terms of other classes. For example, mountain bikes, road bikes, and tandems are all types of bicycles. In object-oriented terminology, mountain bikes, road bikes, and tandems are all<a href="javascript:var meth=openWin; meth('subclass');" onMouseOver="self.status='Look up subclasses in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>subclasses</em></font></a><a href="javascript:var meth=openWin; meth('subclass');" onMouseOver="self.status='Look up subclasses 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 bicycle class. Similarly, the bicycle class is the<a href="javascript:var meth=openWin; meth('superclass');" onMouseOver="self.status='Look up superclass in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>superclass</em></font></a><a href="javascript:var meth=openWin; meth('superclass');" onMouseOver="self.status='Look up superclass 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 mountain bikes, road bikes, and tandems. This relationship is shown in<span id="figure:concepts-bikeHierarchy.gif">the following figure</span>.<center><p><IMG SRC="../../figures/java/concepts-bikeHierarchy.gif" WIDTH="466" HEIGHT="350" ALIGN="BOTTOM" ALT="A diagram of classes in a hierarchy."></p><p class="FigureCaption">The hierarchy of bicycle classes.</p></center>Each subclass <a href="javascript:var meth=openWin; meth('inheritance');" onMouseOver="self.status='Look up inherits in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>inherits</em></font></a><a href="javascript:var meth=openWin; meth('inheritance');" onMouseOver="self.status='Look up inherits 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> state (in the form of variable declarations) from the superclass. Mountain bikes, road bikes, and tandems share some states: cadence, speed, and the like. Also, each subclass inherits methods from the superclass. Mountain bikes, road bikes, and tandems share some behaviors &#151; braking and changing pedaling speed, for example.<p>However, subclasses are not limited to the states and behaviors provided to them by their superclass. Subclasses can add variables and methods to the ones they inherit from the superclass. Tandem bicycles have two seats and two sets of handlebars; some mountain bikes have an additional chain ring,giving them a lower gear ratio.<p>Subclasses can also<a href="javascript:var meth=openWin; meth('overriding');" onMouseOver="self.status='Look up override in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>override</em></font></a><a href="javascript:var meth=openWin; meth('overriding');" onMouseOver="self.status='Look up override 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> inherited methods and provide specialized implementations for those methods. For example, if you had a mountain bike with an additional chain ring, you could override the "change gears" method so that the rider could shift into those lower gears.<p>You are not limited to just one layer of inheritance. The inheritance tree,or <a href="javascript:var meth=openWin; meth('hierarchy');" onMouseOver="self.status='Look up class hierarchy in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>class hierarchy</em></font></a><a href="javascript:var meth=openWin; meth('hierarchy');" onMouseOver="self.status='Look up class hierarchy 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>, can be as deep as needed. Methods and variables are inherited down through the levels. In general, the farther down in the hierarchy a class appears, the more specialized its behavior.<P><blockquote><hr><strong>Note:</strong>&nbsp;Class hierarchies should reflect what the classes <em>are</em>,not how they're <em>implemented</em>.When implementing a tricycle class,it might be convenient to make ita subclass of the bicycle class &#151;after all, both tricycles and bicycles havea current speed and cadence. However,because a tricycle is not a bicycle,it's unwise to publicly tie the two classes together.It could confuse users,make the tricycle class have methods(for example, to change gears)that it doesn't need,and make updating or improving the tricycle class difficult.<hr></blockquote><p>The <CODE>Object</CODE> class is at the top of class hierarchy, and each class is its descendant (directly or indirectly). A variable of type <CODE>Object</CODE> can hold a reference to any object, such as an instance of a class or an array. <CODE>Object</CODE> provides behaviors that are shared by all objects running in the Java Virtual Machine. For example, all classes inherit <CODE>Object</CODE>'s <CODE>toString</CODE> method, which returns a string representation of the object. The <a target="_top" href="../javaOO/subclasses.html">Managing Inheritance</a><a target="_top" href="../javaOO/subclasses.html"><img src="../../images/tutorialIcon.gif" width=11 height=11 border=0 align="MIDDLE" alt=" (in the Learning the Java Language trail)"></a> section covers the <code>Object</code> class in detail. <P>Inheritance offers the following benefits:<UL><LI>Subclasses provide specialized behaviors from the basis of common elements provided by the superclass. Through the use of inheritance, programmers can reuse the code in the superclass many times.</LI> <LI>Programmers can implement superclasses called <a href="javascript:var meth=openWin; meth('abstract class');" onMouseOver="self.status='Look up abstract classes in glossary'; return true;" onMouseOut="self.status=''; return true;"><font color="#00bb00"><em>abstract classes</em></font></a><a href="javascript:var meth=openWin; meth('abstract class');" onMouseOver="self.status='Look up abstract classes 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> that define common behaviors. The abstract superclassdefines and may partially implement the behavior, butmuch of the class is undefined and unimplemented.Other programmers fill in the details with specializedsubclasses.</LI></UL></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="class.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="interface.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>&gt;<a href="../index.html" target="_top">Start of Trail</a>&gt;<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 + -