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

📄 8.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<p><a name="228059"></a>
It is permitted for a single method declaration in a class to implement methods of more than one superinterface. For example, in the code:<p>
<pre><br><a name="228063"></a>interface Fish { int getNumberOfScales(); }
<br><br><a name="228065"></a>interface Piano { int getNumberOfScales(); }
<br></pre><pre><a name="228061"></a>
class Tuna implements Fish, Piano {
<a name="228070"></a>	// You can tune a piano, but can you tuna fish?
<a name="228069"></a>	int getNumberOfScales() { return 91; }
<a name="228068"></a>}
</pre><a name="228071"></a>
the method <code>getNumberOfScales</code> in class <code>Tuna</code> has a name, signature, and return 
type that matches the method declared in interface <code>Fish</code> and also matches the 
method declared in interface <code>Piano</code>; it is considered to implement both.
<p><a name="228080"></a>
On the other hand, in a situation such as this:<p>
<pre><br><a name="228083"></a>interface Fish { int getNumberOfScales(); }
<br><br><a name="228084"></a>interface StringBass { double getNumberOfScales(); }
<br></pre><pre><a name="228085"></a>
class Bass implements Fish, StringBass {
<a name="228086"></a>	// This declaration cannot be correct, no matter what type is used.
<a name="228087"></a>	public ??? getNumberOfScales() { return 91; }
<a name="228088"></a>}
</pre><a name="228081"></a>
it is impossible to declare a method named <code>getNumberOfScales</code> with the same 
signature and return type as those of both the methods declared in interface <code>Fish</code> 
and in interface <code>StringBass</code>, because a class can have only one method with a 
given signature <a href="8.doc.html#40420">(&#167;8.4)</a>. Therefore, it is impossible for a single class to implement 
both interface <code>Fish</code> and interface <code>StringBass</code> <a href="8.doc.html#228745">(&#167;8.4.6)</a>.
<p><a name="18988"></a>
<h3>8.1.5    Class Body and Member Declarations</h3>
<a name="77979"></a>
A <i>class body</i> may contain declarations of members of the class, that is, fields 
<a href="8.doc.html#40898">(&#167;8.3)</a> and methods <a href="8.doc.html#40420">(&#167;8.4)</a>. A class body may also contain static initializers <a href="8.doc.html#39245">(&#167;8.5)</a> 
and declarations of constructors <a href="8.doc.html#41652">(&#167;8.6)</a> for the class.
<p><ul><pre>
<i>ClassBody:<br>
	</i><code>{ </code><i>ClassBodyDeclarations</i><sub><i>opt</i></sub><code> }
</code>
<i>ClassBodyDeclarations:<br>
</i>	<i>ClassBodyDeclaration<br>
</i>	<i>ClassBodyDeclarations</i><code> </code><i>ClassBodyDeclaration
</i>
<i>ClassBodyDeclaration:<br>
	ClassMemberDeclaration<br>
	StaticInitializer<br>
	ConstructorDeclaration
</i>
<i>ClassMemberDeclaration:<br>
	FieldDeclaration<br>
</i>	<i>MethodDeclaration
</i></pre></ul><a name="14175"></a>
The scope of the name of a member declared in or inherited by a class type is the 
entire body of the class type declaration.
<p><a name="21831"></a>
<h2>8.2    Class Members</h2>
<a name="40942"></a>
The members of a class type are all of the following:
<p><ul><a name="40946"></a>
<li>Members inherited from its direct superclass <a href="8.doc.html#21723">(&#167;8.1.3)</a>, except in class <code>Object</code>, which has no direct superclass
<a name="45862"></a>
<li>Members inherited from any direct superinterfaces <a href="8.doc.html#34031">(&#167;8.1.4)</a>
<a name="45866"></a>
<li>Members declared in the body of the class <a href="8.doc.html#18988">(&#167;8.1.5)</a>
</ul><a name="30394"></a>
Members of a class that are declared <code>private</code> are not inherited by subclasses of that class. Only members of a class that are declared <code>protected</code> or <code>public</code> are inherited by subclasses declared in a package other than the one in which the class is declared.<p>
<a name="230007"></a>
Constructors and static initializers are not members and therefore are not inherited.<p>
<a name="36731"></a>
The example:<p>
<pre><a name="14608"></a>
class Point {
<a name="14609"></a>	int x, y;
<a name="36739"></a>	private Point() { reset(); }
<a name="14610"></a>	Point(int x, int y) { this.x = x; this.y = y; }
<a name="16317"></a>	private void reset() { this.x = 0; this.y = 0; }
<a name="14611"></a>}
<br><a name="14612"></a>
class ColoredPoint extends Point {
<a name="36760"></a>	int color;
<a name="36761"></a>	void clear() { reset(); }														// error
<a name="36762"></a>}
<br><a name="14615"></a>
class Test {
<a name="14616"></a>	public static void main(String[] args) {
<a name="14617"></a>		ColoredPoint c = new ColoredPoint(0, 0);													// error
<a name="16316"></a>		c.reset();													// error
<a name="14618"></a>	}
<a name="14619"></a>}
</pre><a name="14620"></a>
causes four compile-time errors:
<p><ul><a name="16328"></a>
<li>An error occurs because <code>ColoredPoint</code> has no constructor declared with two integer parameters, as requested by the use in <code>main</code>. This illustrates the fact that <code>ColoredPoint</code> does not inherit the constructors of its superclass <code>Point</code>.
<a name="16329"></a>
<li>Another error occurs because <code>ColoredPoint</code> declares no constructors, and therefore a default constructor for it is automatically created <a href="8.doc.html#16823">(&#167;8.6.7)</a>, and this default constructor is equivalent to:
<a name="14624"></a>	ColoredPoint() { super(); }
</ul><ul><a name="14625"></a>
<br><br>which invokes the constructor, with no arguments, for the direct superclass of the class <code>ColoredPoint</code>. The error is that the constructor for <code>Point</code> that takes no arguments is <code>private</code>, and therefore is not accessible outside the class <code>Point</code>, even through a superclass constructor invocation <a href="8.doc.html#78435">(&#167;8.6.5)</a>.
</ul><ul><a name="16327"></a>
<li>Two more errors occur because the method <code>reset</code> of class <code>Point</code> is <code>private</code>, and therefore is not inherited by class <code>ColoredPoint</code>. The method invocations in method <code>clear</code> of class <code>ColoredPoint</code> and in method <code>main</code> of class <code>Test</code> are therefore not correct.
</ul><a name="30229"></a>
<h3>8.2.1    Examples of Inheritance</h3>
<a name="36776"></a>
This section illustrates inheritance of class members through several examples.
<p><a name="40830"></a>
<h4>8.2.1.1    Example: Inheritance with Default Access</h4>
<a name="40831"></a>
Consider the example where the <code>points</code> package declares two compilation units:
<p><pre><br><a name="40832"></a>package points;
<br></pre><pre><a name="40833"></a>
public class Point {
<a name="40834"></a>	int x, y;<br>
	public void move(int dx, int dy) { x += dx; y += dy; }
<a name="40835"></a>}
</pre><a name="40836"></a>
and:
<p><pre><br><a name="40837"></a>package points;
<br></pre><pre><a name="40838"></a>
public class Point3d extends Point {
<a name="40839"></a>	int z;
<a name="40840"></a>	public void move(int dx, int dy, int dz) {
<a name="40841"></a>		x += dx; y += dy; z += dz;
<a name="40842"></a>	}
<a name="40843"></a>}
</pre><a name="40844"></a>
and a third compilation unit, in another package, is:
<p><pre><br><a name="40845"></a>import points.Point3d;
<br></pre><pre><a name="40846"></a>
class Point4d extends Point3d {
<a name="40847"></a>	int w;
<a name="40848"></a>	public void move(int dx, int dy, int dz, int dw) {
<a name="40849"></a>		x += dx; y += dy; z += dz; w += dw; // compile-time errors
<a name="40850"></a>	}
<a name="40851"></a>}
</pre><a name="40852"></a>
Here both classes in the <code>points</code> package compile. The class <code>Point3d</code> inherits the 
fields <code>x</code> and <code>y</code> of class <code>Point</code>, because it is in the same package as <code>Point</code>. The 
class <code>Point4d</code>, which is in a different package, does not inherit the fields <code>x</code> and <code>y</code> 
of class <code>Point</code> or the field <code>z</code> of class <code>Point3d</code>, and so fails to compile.
<p><a name="40853"></a>
A better way to write the third compilation unit would be:<p>
<pre><br><a name="40854"></a>import points.Point3d;
<br></pre><pre><a name="40855"></a>
class Point4d extends Point3d {
<a name="40856"></a>	int w;
<a name="40857"></a>	public void move(int dx, int dy, int dz, int dw) {
<a name="40858"></a>		super.move(dx, dy, dz); w += dw;
<a name="40859"></a>	}
<a name="40860"></a>}
</pre><a name="40861"></a>
using the <code>move</code> method of the superclass <code>Point3d</code> to process <code>dx</code>, <code>dy</code>, and <code>dz</code>. If 
<code>Point4d</code> is written in this way it will compile without errors.
<p><a name="40862"></a>
<h4>8.2.1.2    Inheritance with public and protected</h4>
<a name="40863"></a>
Given the class <code>Point</code>:
<p><pre><br><a name="40864"></a>package points;
<br></pre><pre><a name="40865"></a>
public class Point {
<br><a name="40866"></a>	public int x, y;
<br><br><a name="40867"></a>	protected int useCount = 0;
<br><br><a name="40868"></a>	static protected int totalUseCount = 0;
<br><a name="36785"></a>
	public void move(int dx, int dy) {
<a name="40869"></a>		x += dx; y += dy; useCount++; totalUseCount++;
<a name="40870"></a>	}
<br><a name="40871"></a>}
</pre><a name="40872"></a>
the <code>public</code> and <code>protected</code> fields <code>x</code>, <code>y</code>, <code>useCount</code> and <code>totalUseCount</code> are inherited
in all subclasses of <code>Point</code>. Therefore, this test program, in another package, 
can be compiled successfully:
<p><pre><a name="40873"></a>
class Test extends points.Point {
<a name="40874"></a>	public void moveBack(int dx, int dy) {
<a name="40875"></a>		x -= dx; y -= dy; useCount++; totalUseCount++;
<a name="40876"></a>	}
<a name="40877"></a>}
</pre><a name="40879"></a>
<h4>8.2.1.3    Inheritance with private</h4>
<a name="40880"></a>
In the example:
<p><pre><a name="40881"></a>
class Point {
<br><a name="40882"></a>	int x, y;
<br><a name="40883"></a>
	void move(int dx, int dy) {
<a name="40884"></a>		x += dx; y += dy; totalMoves++;
<a name="40885"></a>	}
<br><br><a name="40886"></a>	private static int totalMoves;
<br><br><a name="40887"></a>	void printMoves() { System.out.println(totalMoves); }
<br><a name="40888"></a>}
<br><a name="40889"></a>
class Point3d extends Point {
<br><a name="40890"></a>	int z;
<br><a name="40891"></a>
	void move(int dx, int dy, int dz) {
<a name="40892"></a>		super.move(dx, dy); z += dz; totalMoves++;
<a name="40893"></a>	}
<br><a name="40894"></a>}
</pre><a name="40822"></a>

⌨️ 快捷键说明

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