📄 068-071.html
字号:
<HTML>
<HEAD>
<META name=vsisbn content="1571690433"><META name=vstitle content="Black Art of Java Game Programming"><META name=vsauthor content="Joel Fan"><META name=vsimprint content="Sams"><META name=vspublisher content="Macmillan Computer Publishing"><META name=vspubdate content="11/01/96"><META name=vscategory content="Web and Software Development: Programming, Scripting, and Markup Languages: Java"><TITLE>Black Art of Java Game Programming:Using Objects for Animations</TITLE>
<!-- HEADER --><STYLE type="text/css"> <!-- A:hover { color : Red; } --></STYLE><META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW"><script><!--function displayWindow(url, width, height) { var Win = window.open(url,"displayWindow",'width=' + width +',height=' + height + ',resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></script><SCRIPT><!--function popUp(url) { var Win = window.open(url,"displayWindow",'width=400,height=300,resizable=1,scrollbars=yes'); if (Win) { Win.focus(); }}//--></SCRIPT><script language="JavaScript1.2"><!--function checkForQuery(fm) { /* get the query value */ var i = escape(fm.query.value); if (i == "") { alert('Please enter a search word or phrase'); return false; } /* query is blank, dont run the .jsp file */ else return true; /* execute the .jsp file */}//--></script></HEAD><BODY>
<TABLE border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=75 valign=top>
<img src="../1571690433.gif" width=60 height=73 alt="Black Art of Java Game Programming" border="1">
</td>
<td align="left">
<font face="arial, helvetica" size="-1" color="#336633"><b>Black Art of Java Game Programming</b></font>
<br>
<font face="arial, helvetica" size="-1"><i>by Joel Fan</i>
<br>
Sams, Macmillan Computer Publishing
<br>
<b>ISBN:</b> 1571690433<b> Pub Date:</b> 11/01/96</font>
</td>
</tr>
</table>
<P>
<!--ISBN=1571690433//-->
<!--TITLE=Black Art of Java Game Programming//-->
<!--AUTHOR=Joel Fan//-->
<!--AUTHOR=Eric Ries//-->
<!--AUTHOR=Calin Tenitchi//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=2//-->
<!--PAGES=068-071//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="064-068.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="071-075.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The second kind of is-a relationship is called <I>specialization</I>. In this case, the subclass usually refines or specializes methods found in the base class. For example, musicians play instruments of one kind or another, and a Musician class would have a method play() to produce this behavior. Now, a pianist is a type of musician, as is a violinist, or a conductor, but each has different methods of playing music, and so as subclasses, each denotes a different specialization of the Musician class.</P>
<H4 ALIGN="LEFT"><A NAME="Heading18"></A><FONT COLOR="#000077">When Not to Use Inheritance</FONT></H4>
<P>Inheritance is not appropriate for “has-a” relationships. A boat, for example, has a rudder; however, inheriting a Boat class from a Rudder class isn’t the proper way of reusing the Rudder code. Instead, the Boat class should have an instance of a Rudder object, to capture the has-a relationship. Boat is called a <I>container</I> class, since it contains Rudder.</P>
<P>Sometimes the relationship between classes isn’t so easy to define, and it might have elements of extension and specialization, as well as containership. In this case, you must experiment to find out what works.</P>
<H4 ALIGN="LEFT"><A NAME="Heading19"></A><FONT COLOR="#000077">Inheritance Details</FONT></H4>
<P>In this section, we’ll cover further details of using inheritance in Java:
</P>
<DL>
<DD><B>•</B> The Object class, which is the root of the Java object system
<DD><B>•</B> Method overriding
<DD><B>•</B> Using the super keyword
<DD><B>•</B> Final classes and methods
</DL>
<P>Feel free to skim this section and come back when needed.
</P>
<H4 ALIGN="CENTER"><A NAME="Heading20"></A><FONT COLOR="#000077">The Object Class</FONT></H4>
<P>The Object class defined in package java.lang is the root of Java’s class hierarchy; all other classes are subclasses of Object. If a superclass isn’t specified by a class declaration (with the <I>extends</I> keyword), the class extends Object by default. This means that the public and protected methods of Object are inherited for all objects.</P>
<P>For example, even the “empty” class</P>
<!-- CODE SNIP //-->
<PRE>
// class A automatically extends java.lang.Object
class A {
(c) // nothing here!
}
</PRE>
<!-- END CODE SNIP //-->
<P>inherits the public and protected methods defined in Object. Since an instance of any class “is an” object, this implicit inheritance makes sense, and it serves to unify Java’s object system. To implement a collection of generic objects, for example, you can use an array of Object.
</P>
<P>The Object methods, which we won’t use in this chapter, contain facilities for comparing equality of objects, cloning objects, synchronizing threads, and more.</P>
<H4 ALIGN="CENTER"><A NAME="Heading21"></A><FONT COLOR="#000077">Method Overriding</FONT></H4>
<P>Sometimes it makes sense for one class to inherit behaviors from another, but you’d like to redefine, or <I>override</I>, some of the inherited behaviors. This situation comes up often in specialization relationships. For example, a Pianist class inherits the play() method from a Musician class, but it can override play() to provide the specific behavior desired.</P>
<P>To override an inherited method, simply redefine it in the subclass. The overriding method must have the same method signature as the original version. In the following, Pianist overrides the play(int) method from its superclass, but doesn’t override starve().</P>
<!-- CODE //-->
<PRE>
class Musician {
public int yearsOfStruggle;
public void play(int howLong) {
...
}
public void starve() {
...
}
}
class Pianist extends Musician {
// override play(int) method from Musician class
public void play(int howLong) {
...
}
}
</PRE>
<!-- END CODE //-->
<P>Thus, an instance of Pianist will use the play(int) method defined in Pianist, and the starve() method from Musician. Any class that inherits from Pianist (say a VirtuosoPianist) will inherit the play(int) method defined in Pianist.
</P>
<P>Sometimes you’ve overridden a method, but you still need to invoke the version from the superclass. This is possible with the <I>super</I> keyword.</P>
<H4 ALIGN="CENTER"><A NAME="Heading22"></A><FONT COLOR="#000077">The super Keyword</FONT></H4>
<P>The keyword <I>super</I>, used within the definition of a class, allows you to reference the variables and methods available to the superclass. As an illustration, let’s subclass a VirtuosoPianist from the Pianist class defined in the preceding section:</P>
<!-- CODE //-->
<PRE>
class VirtuosoPianist extends Pianist {
public long yearsOfStruggle = 21L;
// override play(int) method from Pianist class
public void play(int howLong) {
...
// call play(int) method defined in superclass
super.play(howLong * 137);
// refer to variable defined in this class
long i = yearsOfStruggle;
// refer to variable from superclass
int j = super.yearsOfStruggle;
}
}
</PRE>
<!-- END CODE //-->
<P>The play(int) method of the class (VirtuosoPianist) invokes the play(int) method that’s available to the superclass (Pianist) by using <I>super</I>.</P>
<P>Furthermore, using <I>super</I>, you can reference variables available to the superclass, but hidden by the class. For example, the variable <I>yearsOfStruggle</I> defined in VirtuosoPianist (a long) hides the variable of the same name. The <I>super</I> keyword allows you to access the <I>yearsOfStruggle</I> (an int) inherited through the superclass Pianist.</P>
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Using super in Constructors</FONT></H4>
<P>You can also use <I>super</I> in a constructor to invoke a superclass constructor. In the following, the constructor of B calls a constructor of its superclass A by using <I>super</I> followed by the appropriate argument list.</P>
<!-- CODE //-->
<PRE>
class A {
// constructor #1
public A(int x) {
// implicit super() here
x++;
...
}
// constructor #2
public A(float x,int y) {
// NO implicit super() here
this(y); // call constructor #1
y++;
...
}
}
class B extends A {
public B() {
super((float)13, 17); // call A’s constructor #2
...
}
}
</PRE>
<!-- END CODE //-->
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="064-068.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="071-075.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</BODY>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -