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

📄 6.doc.html

📁 java语言规范
💻 HTML
📖 第 1 页 / 共 5 页
字号:
The members of a class type <a href="8.doc.html#21831">(&#167;8.2)</a> are fields and methods. The members of a 
class type are all of the following:
<p><ul><a name="34768"></a>
<li>Members inherited from its direct superclass <a href="8.doc.html#21723">(&#167;8.1.3)</a>, if it has one (the class <code>Object</code> has no direct superclass)
<a name="34777"></a>
<li>Members inherited from any direct superinterfaces <a href="8.doc.html#34031">(&#167;8.1.4)</a>
<a name="34778"></a>
<li>Members declared in the body of the class <a href="8.doc.html#18988">(&#167;8.1.5)</a>
</ul><a name="62337"></a>
Constructors <a href="8.doc.html#41652">(&#167;8.6)</a> are not members.
<p><a name="31685"></a>
There is no restriction against a field and a method of a class type having the same simple name.<p>
<a name="56650"></a>
A class may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any of the fields by its simple name results in a compile-time error (<a href="6.doc.html#54547">&#167;6.5.6.2</a>, <a href="8.doc.html#21831">&#167;8.2</a>).<p>
<a name="31689"></a>
In the example:<p>
<pre><a name="25284"></a>
interface Colors {
<a name="25285"></a>	int WHITE = 0, BLACK = 1;
<a name="25286"></a>}
<a name="31681"></a>
interface Separates {
<a name="25287"></a>	int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3;
<a name="25288"></a>}
<a name="25289"></a>
class Test implements Colors, Separates {
<a name="25290"></a>	public static void main(String[] args) {
<a name="25291"></a>		System.out.println(BLACK); // compile-time error: ambiguous
<a name="25292"></a>	}
<a name="25293"></a>}
</pre><a name="25294"></a>
the name <code>BLACK</code> in the method <code>main</code> is ambiguous, because class <code>Test</code> has two 
members named <code>BLACK</code>, one inherited from <code>Colors</code> and one from <code>Separates</code>.
<p><a name="23306"></a>
A class type may have two or more methods with the same simple name if the methods have different signatures <a href="8.doc.html#38649">(&#167;8.4.2)</a>, that is, if they have different numbers of parameters or different parameter types in at least one parameter position. Such a method member name is said to be <i>overloaded</i>.<p>
<a name="31700"></a>
A class type may contain a declaration for a method with the same name and the same signature as a method that would otherwise be inherited from a superclass or superinterface. In this case, the method of the superclass or superinterface is not inherited. If the method not inherited is <code>abstract</code>, then the new declaration is said to <i>implement</i> it; if the method not inherited is not <code>abstract</code>, then the new declaration is said to <i>override</i> it.<p>
<a name="31699"></a>
In the example:<p>
<pre><a name="38566"></a>
class Point {
<a name="38567"></a>	float x, y;
<a name="38568"></a>	void move(int dx, int dy) { x += dx; y += dy; }
<a name="31704"></a>	void move(float dx, float dy) { x += dx; y += dy; }
<a name="31705"></a>	public String toString() { return "("+x+","+y+")"; }
<a name="31706"></a>}
</pre><a name="31707"></a>
the class <code>Point</code> has two members that are methods with the same name, <code>move</code>. 
The overloaded <code>move</code> method of class <code>Point</code> chosen for any particular method 
invocation is determined at compile time by the overloading resolution procedure 
given in <a href="15.doc.html#20448">&#167;15.11</a>.
<p><a name="62345"></a>
In this example, the members of the class <code>Point</code> are the <code>float</code> instance variables <code>x</code> and <code>y</code> declared in <code>Point</code>, the two declared <code>move</code> methods, the declared <code>toString</code> method, and the members that <code>Point</code> inherits from its implicit direct superclass <code>Object</code> <a href="4.doc.html#11055">(&#167;4.3.2)</a>, such as the method <code>hashCode</code> <a href="javalang.doc1.html#13784">(&#167;20.1.4)</a>. Note that <code>Point</code> does not inherit the <code>toString</code> method <a href="javalang.doc1.html#1152">(&#167;20.1.2)</a> of class <code>Object</code> because that method is overridden by the declaration of the <code>toString</code> method in class <code>Point</code>.<p>
<a name="34849"></a>
<h3>6.4.3    The Members of an Interface Type</h3>
<a name="31765"></a>
The members of an interface type <a href="9.doc.html#32392">(&#167;9.2)</a> are fields and methods. The members of 
an interface are all of the following:
<p><ul><a name="31784"></a>
<li>Members inherited from any direct superinterfaces <a href="9.doc.html#78598">(&#167;9.1.3)</a>
<a name="31788"></a>
<li>Members declared in the body of the interface <a href="9.doc.html#236431">(&#167;9.1.4)</a>
</ul><a name="31831"></a>
An interface may have two or more fields with the same simple name if they are declared in different interfaces and inherited. An attempt to refer to any such field by its simple name results in a compile-time error (<a href="6.doc.html#22022">&#167;6.5.5.1</a>, <a href="9.doc.html#32392">&#167;9.2</a>).<p>
<a name="61795"></a>
In the example:<p>
<pre><a name="20498"></a>
interface Colors {
<a name="20499"></a>	int WHITE = 0, BLACK = 1;
<a name="20500"></a>}
<a name="31851"></a>
interface Separates {
<a name="20501"></a>	int CYAN = 0, MAGENTA = 1, YELLOW = 2, BLACK = 3;
<a name="20502"></a>}
<a name="35253"></a>interface ColorsAndSeparates extends Colors, Separates {<br>
	int DEFAULT = BLACK;								 	// compile-time error: ambiguous<br>
}
</pre><a name="35254"></a>
the members of the interface <code>ColorsAndSeparates</code> include those members 
inherited from <code>Colors</code> and those inherited from <code>Separates</code>, namely <code>WHITE</code>, 
<code>BLACK</code> (first of two), <code>CYAN</code>, <code>MAGENTA</code>, <code>YELLOW</code>, and <code>BLACK</code> (second of two). The 
member name <code>BLACK</code> is ambiguous in the interface <code>ColorsAndSeparates</code>.
<p><a name="27725"></a>
<h3>6.4.4    The Members of an Array Type</h3>
<a name="36005"></a>
The members of an array type <a href="10.doc.html#11364">(&#167;10.7)</a> are all of the following:
<p><ul><a name="23342"></a>
<li>Members inherited from its implicit superclass <code>Object</code> (<a href="4.doc.html#11055">&#167;4.3.2</a>, <a href="javalang.doc1.html#46442">&#167;20.1</a>)
<a name="23344"></a>
<li>The field <code>length</code>, which is a constant (<code>final</code>) field of every array; its type is <code>int</code> and it contains the number of components of the array
</ul><a name="27727"></a>
The example:
<p><pre><a name="27728"></a>
class Test {
<a name="27729"></a>	public static void main(String[] args) {
<a name="27730"></a>		int[] ia = new int[3];
<a name="27731"></a>		int[] ib = new int[6];
<a name="27733"></a>		System.out.println(ia.getClass() == ib.getClass());
<a name="31873"></a>		System.out.println("ia has length=" + ia.length);
<a name="27734"></a>	}
<a name="27735"></a>}
</pre><a name="27736"></a>
produces the output:
<p><pre><a name="31879"></a>
true
<a name="27737"></a>ia has length=3
</pre><a name="27739"></a>
This example uses the method <code>getClass</code> inherited from class <code>Object</code> and the 
field <code>length</code>. The result of the comparison of the <code>Class</code> objects in the second 
<code>println</code> demonstrates that all arrays whose components are of type <code>int</code> are 
instances of the same array type, which is <code>int[]</code>.
<p><a name="20569"></a>
<h2>6.5    Determining the Meaning of a Name</h2>
<a name="22344"></a>
The meaning of a name in Java depends on the context in which it is used. The 
determination of the meaning of a name requires three steps. First, context causes 
a name syntactically to fall into one of five categories: <i>PackageName</i>, <i>TypeName</i>, 
<i>ExpressionName</i>, <i>MethodName</i>, or <i>AmbiguousName</i>. Second, a name that is initially
classified by its context as an <i>AmbiguousName</i> is then reclassified by certain 
scoping rules to be a <i>PackageName</i>, <i>TypeName</i>, or <i>ExpressionName</i>. Third, the 
resulting category then dictates the final determination of the meaning of the name 
(or a compilation error if the name has no meaning).
<p><ul><pre>
<i>PackageName:<br>
</i>	<i>Identifier<br>
</i>	<i>PackageName</i><code> . </code><i>Identifier
</i>
<i>TypeName:<br>
</i>	<i>Identifier<br>
</i>	<i>PackageName</i><code> . </code><i>Identifier
</i>
<i>ExpressionName:<br>
</i>	<i>Identifier<br>
</i>	<i>AmbiguousName</i><code> . </code><i>Identifier
</i>
<i>MethodName:<br>
</i>	<i>Identifier<br>
</i>	<i>AmbiguousName</i><code> . </code><i>Identifier
</i>
<i>AmbiguousName:<br>
</i>	<i>Identifier<br>
</i>	<i>AmbiguousName</i><code> . </code><i>Identifier
</i></pre></ul><a name="32744"></a>
Java's use of context helps to minimize name conflicts between entities of different kinds. Such conflicts will be rare if the naming conventions described in <a href="6.doc.html#11186">&#167;6.8</a> are followed. Nevertheless, conflicts may arise unintentionally as types developed by different programmers or different organizations evolve. For example, types, methods, and fields may have the same name. Java never has trouble distinguishing between a method and a field with the same name, since the context of a use always tells whether a method or a field is intended.<p>
<a name="32740"></a>
<h3>6.5.1    Syntactic Classification of a Name According to Context</h3>
<a name="32741"></a>
A name is syntactically classified as a <i>PackageName</i> in these contexts:
<p><ul><a name="32159"></a>
<li>In a package declaration <a href="7.doc.html#26619">(&#167;7.4)</a>
<a name="32164"></a>
<li>In a type-import-on-demand declaration <a href="7.doc.html#26725">(&#167;7.5.2)</a>
<a name="32492"></a>
<li>To the left of the "<code>.</code>" in a qualified <i>PackageName</i>
<a name="32494"></a>
<li>To the left of the "<code>.</code>" in a qualified <i>TypeName</i>
</ul><a name="32149"></a>
A name is syntactically classified as a <i>TypeName</i> in these contexts:
<p><ul><a name="32174"></a>
<li>In a single-type-import declaration <a href="7.doc.html#26699">(&#167;7.5.1)</a>
<a name="32178"></a>
<li>In an <code>extends</code> clause in a class declaration <a href="8.doc.html#21723">(&#167;8.1.3)</a>
<a name="32182"></a>
<li>In an <code>implements</code> clause in a class declaration <a href="8.doc.html#34031">(&#167;8.1.4)</a>
<a name="34302"></a>
<li>In an <code>extends</code> clause in an interface declaration <a href="9.doc.html#78598">(&#167;9.1.3)</a>
<a name="32290"></a>
<li>As a <i>Type</i> (or the part of a <i>Type</i> that remains after all brackets are deleted) in any of the following contexts:
<ul>
<a name="32310"></a>
<li>In a field declaration (<a href="8.doc.html#40898">&#167;8.3</a>, <a href="9.doc.html#78642">&#167;9.3</a>)
<a name="32322"></a>
<li>As the result type of a method (<a href="8.doc.html#40420">&#167;8.4</a>, <a href="9.doc.html#78651">&#167;9.4</a>)
<a name="32355"></a>
<li>As the type of a formal parameter of a method or constructor (<a href="8.doc.html#38698">&#167;8.4.1</a>, <a href="8.doc.html#29488">&#167;8.6.1</a>, <a href="9.doc.html#78651">&#167;9.4</a>)
<a name="32266"></a>
<li>As the type of an exception that can be thrown by a method or constructor (<a href="8.doc.html#78323">&#167;8.4.4</a>, <a href="8.doc.html#244611">&#167;8.6.4</a>, <a href="9.doc.html#78651">&#167;9.4</a>)
<a name="32224"></a>
<li>As the type of a local variable <a href="14.doc.html#5920">(&#167;14.3)</a>
<a name="32225"></a>
<li>As the type of an exception parameter in a <code>catch</code> clause of a <code>try</code> statement <a href="14.doc.html#79311">(&#167;14.18)</a>

⌨️ 快捷键说明

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