📄 tij310.htm
字号:
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:Month.java</font>
<font color=#009900>// A more robust enumeration system.</font>
<font color=#0000ff>package</font> c08;
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>public</font> <font color=#0000ff>final</font> <font color=#0000ff>class</font> Month {
<font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<font color=#0000ff>private</font> String name;
<font color=#0000ff>private</font> Month(String nm) { name = nm; }
<font color=#0000ff>public</font> String toString() { <font color=#0000ff>return</font> name; }
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> Month
JAN = <font color=#0000ff>new</font> Month(<font color=#004488>"January"</font>),
FEB = <font color=#0000ff>new</font> Month(<font color=#004488>"February"</font>),
MAR = <font color=#0000ff>new</font> Month(<font color=#004488>"March"</font>),
APR = <font color=#0000ff>new</font> Month(<font color=#004488>"April"</font>),
MAY = <font color=#0000ff>new</font> Month(<font color=#004488>"May"</font>),
JUN = <font color=#0000ff>new</font> Month(<font color=#004488>"June"</font>),
JUL = <font color=#0000ff>new</font> Month(<font color=#004488>"July"</font>),
AUG = <font color=#0000ff>new</font> Month(<font color=#004488>"August"</font>),
SEP = <font color=#0000ff>new</font> Month(<font color=#004488>"September"</font>),
OCT = <font color=#0000ff>new</font> Month(<font color=#004488>"October"</font>),
NOV = <font color=#0000ff>new</font> Month(<font color=#004488>"November"</font>),
DEC = <font color=#0000ff>new</font> Month(<font color=#004488>"December"</font>);
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> Month[] month = {
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC
};
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>final</font> Month number(<font color=#0000ff>int</font> ord) {
<font color=#0000ff>return</font> month[ord - 1];
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
Month m = Month.JAN;
System.out.println(m);
m = Month.number(12);
System.out.println(m);
System.out.println(m == Month.DEC);
System.out.println(m.equals(Month.DEC));
System.out.println(Month.month[3]);
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"January"</font>,
<font color=#004488>"December"</font>,
<font color=#004488>"true"</font>,
<font color=#004488>"true"</font>,
<font color=#004488>"April"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p><b>Month</b> is a <b>final</b> class with a <b>private</b> constructor, so no one can inherit from it or make any instances of it. The only instances are the <b>final static</b> ones created in the class itself: <b>JAN</b>, <b>FEB</b>, <b>MAR</b>, etc. These objects are also used in the array <b>month</b>, which lets you iterate through an array of <b>Month2</b> objects. The <b>number( ) </b>method allows you to select a <b>Month </b>by giving its corresponding month number. In <b>main( )</b> you can see the <a name="Index708"></a>type safety; <b>m</b> is a <b>Month</b> object so it can be assigned only to a <b>Month</b>. The previous example <b>Months.java </b>provided only <b>int</b> values, so an <b>int</b> variable intended to represent a month could actually be given any integer value, which wasn’t very safe. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1128" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>This approach also allows you to use <b>==</b> or <b>equals( )</b> interchangeably, as shown at the end of <b>main( )</b>. This works because there can be only one instance of each value of <b>Month</b>. In Chapter 11 you’ll learn about another way to set up classes so the objects can be compared to each other. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1129" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>There’s also a month field in <b>java.util.Calendar</b>. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0469" title="Send BackTalk Comment">Feedback</a></font><br></p>
<p>Apache’s Jakarta Commons project contains tools to create enumerations similar to what’s shown in the preceding example, but with less effort. See <i>http://jakarta.apache.org/commons</i>, under “lang,” in the package <b>org.apache.commons.lang.enum</b>. This project also has many other potentially useful libraries. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]A0470" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775678"></a><a name="Heading7100"></a>Initializing fields in
interfaces<br></h3>
<p><a name="Index709"></a><a name="Index710"></a>Fields defined in interfaces are automatically <b>static</b> and <b>final</b>. These cannot be “blank finals,” but they can be initialized with nonconstant expressions. For example:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:RandVals.java</font>
<font color=#009900>// Initializing interface fields with</font>
<font color=#009900>// non-constant initializers.</font>
<font color=#0000ff>import</font> java.util.*;
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> RandVals {
Random rand = <font color=#0000ff>new</font> Random();
<font color=#0000ff>int</font> randomInt = rand.nextInt(10);
<font color=#0000ff>long</font> randomLong = rand.nextLong() * 10;
<font color=#0000ff>float</font> randomFloat = rand.nextLong() * 10;
<font color=#0000ff>double</font> randomDouble = rand.nextDouble() * 10;
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>Since the fields are <b>static</b>, they are initialized when the class is first loaded, which happens when any of the fields are accessed for the first time. Here’s a simple test: <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1130" title="Send BackTalk Comment">Feedback</a></font><br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:TestRandVals.java</font>
<font color=#0000ff>import</font> com.bruceeckel.simpletest.*;
<font color=#0000ff>public</font> <font color=#0000ff>class</font> TestRandVals {
<font color=#0000ff>private</font> <font color=#0000ff>static</font> Test monitor = <font color=#0000ff>new</font> Test();
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
System.out.println(RandVals.randomInt);
System.out.println(RandVals.randomLong);
System.out.println(RandVals.randomFloat);
System.out.println(RandVals.randomDouble);
monitor.expect(<font color=#0000ff>new</font> String[] {
<font color=#004488>"%% -?\\d+"</font>,
<font color=#004488>"%% -?\\d+"</font>,
<font color=#004488>"%% -?\\d\\.\\d+E?-?\\d+"</font>,
<font color=#004488>"%% -?\\d\\.\\d+E?-?\\d+"</font>
});
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE><p><br></p>
<p>The fields, of course, are not part of the interface but instead are stored in the static storage area for that interface. <font size="-2"><a href="mailto:TIJ3@MindView.net?Subject=[TIJ3]Chap08_1131" title="Send BackTalk Comment">Feedback</a></font><br></p>
<h3>
<a name="_Toc24775679"></a><a name="Heading7136"></a>Nesting interfaces<br></h3>
<p><a name="Index711"></a><a name="Index712"></a>Interfaces may be nested within classes and within other interfaces.<sup><a name="fnB34" href="#fn34">[34]</a></sup> This reveals a number of very interesting features:<br></p>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c08:nesting:NestingInterfaces.java</font>
<font color=#0000ff>package</font> c08.nesting;
<font color=#0000ff>class</font> A {
<font color=#0000ff>interface</font> B {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> BImp <font color=#0000ff>implements</font> B {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>private</font> <font color=#0000ff>class</font> BImp2 <font color=#0000ff>implements</font> B {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> C {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>class</font> CImp <font color=#0000ff>implements</font> C {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>private</font> <font color=#0000ff>class</font> CImp2 <font color=#0000ff>implements</font> C {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>private</font> <font color=#0000ff>interface</font> D {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>private</font> <font color=#0000ff>class</font> DImp <font color=#0000ff>implements</font> D {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>public</font> <font color=#0000ff>class</font> DImp2 <font color=#0000ff>implements</font> D {
<font color=#0000ff>public</font> <font color=#0000ff>void</font> f() {}
}
<font color=#0000ff>public</font> D getD() { <font color=#0000ff>return</font> <font color=#0000ff>new</font> DImp2(); }
<font color=#0000ff>private</font> D dRef;
<font color=#0000ff>public</font> <font color=#0000ff>void</font> receiveD(D d) {
dRef = d;
dRef.f();
}
}
<font color=#0000ff>interface</font> E {
<font color=#0000ff>interface</font> G {
<font color=#0000ff>void</font> f();
}
<font color=#009900>// Redundant "public":</font>
<font color=#0000ff>public</font> <font color=#0000ff>interface</font> H {
<font color=#0000ff>void</font> f();
}
<font color=#0000ff>void</font> g();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -