/** * 内部类*///: Parcell.javapublicclassParcell{classContents{privateint i =11;publicintvalue(){return i;}}classDestination{privateString label;Destination(StringwhereTo){ label = whereTo;}StringreadLine(){return label;}}publicvoidship(Stringdest){Contents c =newContents();Destination d =newDestination(dest);System.out.println(d.readLine());}publicstaticvoidmain(String[]args){Parcell p =newParcell();p.ship("Tasmania");}}/** OutputTasmania*/
//: Sequence.java
interface Selector {
boolean end();
Object current();
void next();
}
public class Sequence {
private Object[] items;
private int next = 0;
public Sequence(int size) {
items = new Object[size];
}
public void add(Object x) {
if (next < items.length) {
items[next++] = x;
}
}
private class SequenceSelector implements Selector {
private int i = 0;
public boolean end() {
return i == items.length;
}
public Object current() {
return items[i];
}
public void next() {
if (i < items.length)
i++;
}
}
public Selector selector() {
return new SequenceSelector();
}
public static void main(String args[]) {
Sequence sequence = new Sequence(10);
for (int i = 0; i < 10; ++i) {
sequence.add(Integer.toString(i));
}
Selector selector = sequence.selector();
while(!selector.end()) {
System.out.print(selector.current() + " ");
selector.next();
}
}
} /** Output
0 1 2 3 4 5 6 7 8 9
*/
//: DotThis.java
public class DotThis {
void f() {
System.out.println("DotThis.f()");
}
public class Inner {
public DotThis outer() {
return DotThis.this;
// A plain this would be Inner's this.
}
}
public Inner inner() {
return new Inner();
}
public static void main(String args[]) {
DotThis dt = new DotThis();
DotThis.Inner dti = dt.inner();
dti.outer().f();
}
} /** Output
DotThis.f()
*/
//: DotNew.java
public class DotNew {
public class Inner {}
public static void main(String args[]) {
DotNew dn = new DotNew();
DotNew.Inner dni = dn.new Inner();
}
}
//: DotNew.java
public class DotNew {
public class Inner {
public void f() {
System.out.println("DotNew.Inner.f()");
}
}
public static class StaticInner {
public void f() {
System.out.println("DotNew.StaticInner.f()");
}
}
public Inner f() {
return new Inner(); // 注意这里
}
public static void main(String args[]) {
DotNew dn = new DotNew();
DotNew.Inner dni = dn.new Inner();
dni.f();
//! Inner in = new Inner(); // Error: 无法从静态上下文中引用非静态 变量 this
//! Inner in = new DotNew.Inner(); // Error: 无法从静态上下文中引用非静态 变量 this
Inner in = dn.new Inner();
in.f();
Inner fin = dn.f();
fin.f();
// Inner in2 = new dn.Inner(); // Error: 程序包dn不存在
StaticInner si = new StaticInner();
si.f();
}
} /** Output
DotNew.Inner.f()
DotNew.Inner.f()
DotNew.Inner.f()
DotNew.StaticInner.f()
*/
//: TestParcel.java
interface Destination {
String readLabel(); // 接口中所有成员自动设置为public
}
interface Contents {
int value();
}
class Parcel {
private class PContents implements Contents {
private int i = 0;
public int value() { return i++; }
}
protected class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
public Destination destination(String s) {
return new PDestination(s);
}
public Contents contents() {
return new PContents();
}
}
public class TestParcel {
public static void main(String args[]) {
Parcel p = new Parcel();
Contents c = p.contents();
Destination d = p.destination("Beijing");
System.out.println(c.value());
System.out.println(d.readLabel());
}
} /** Output
0
Beijing
*/
//: Destination.java
public interface Destination {
String readLabel();
}
//: Parcel5.java
public class Parcel5 {
public Destination destination(String s) {
class PDestination implements Destination {
private String label;
private PDestination(String whereTo) {
label = whereTo;
}
public String readLabel() { return label; }
}
return new PDestination(s);
}
public static void main(String args[]) {
Parcel5 p = new Parcel5();
Destination d = p.destination("Tasmania");
System.out.println(d.readLabel());
}
} /** Output
Tasmania
*/
//: Contents.java
public interface Contents {
int value();
}
//: Parcel7.java
// returning an instance of an anonymous inner class
public class Parcel7 {
public Contents contents() {
return new Contents() { // insert a class definition
private int i = 11;
public int value() { return i; }
}; // 这种情况下分号是必须的
}
public static void main(String args[]) {
Parcel7 p = new Parcel7();
Contents c = p.contents();
System.out.println(c.value());
}
} /** Output
11
*/
//: Parcel7b.java
public class Parcel7b {
class MyContents implements Contents {
private int i = 11;
public int value() { return i; }
}
public Contents contents() { return new MyContents(); }
public static void main(String args[]) {
Parcel7b p = new Parcel7b();
Contents c = p.contents();
System.out.println(c.value());
}
} /** Output
11
*/
//: Parcel8.java
public class Parcel8 {
public Wrapping wrapping(int x) {
// Base constructor call
return new Wrapping(x) { // pass Constructor argument
public int value() {
return super.value() * 47;
}
};
}
public static void main(String args[]) {
Parcel8 p = new Parcel8();
Wrapping w = p.wrapping(10);
System.out.println(w.value());
}
} /** Output
470
*/
class Wrapping {
private int i;
public Wrapping(int x) { i = x; }
public int value() { return i; }
}
//: Destination.java
public interface Destination {
String readLabel();
}
//: Parcel10.java
public class Parcel10 {
public Destination destination(final String dest, final float price) {
return new Destination() {
private int cost;
// Instance initialization for each object;
{
System.out.println("Inside instance initializer");
cost = Math.round(price);
if (cost > 100) {
System.out.println("Over budget!");
}
}
private String label = dest;
public String readLabel() { return label; }
};
}
public static void main(String[] args) {
Parcel10 p = new Parcel10();
Destination d = p.destination("Tasmania", 101.223F);
System.out.println(d.readLabel());
}
} /** Output
Inside instance initializer
Over budget!
Tasmania
*/
//: ClassInInterface.java
// {main: ClassInInterface$Test}
public interface ClassInInterface {
void howdy();
class Test implements ClassInInterface {
public void howdy() {
System.out.println("Howdy!");
}
public static void main(String args[]) {
new Test().howdy();
}
}
} /** Output
$ java ClassInInterface\$Test
Howdy!
*/
//: MultiNestingAccess.java
class MNA {
private void e() {
System.out.println("MNA.e()");
}
private void f() {
System.out.println("MNA.f()");
}
class A {
private void g() {
System.out.println("MNA.A.g()");
}
private void f() {
System.out.println("MNA.A.f()");
}
public class B {
void h() {
System.out.println("MNA.A.B.h()");
e();
MNA.this.f(); // 访问最外围类成员
f();
g();
}
}
}
}
public class MultiNestingAccess {
public static void main(String args[]) {
MNA mna = new MNA();
MNA.A mnaa = mna.new A();
MNA.A.B mnaab = mnaa.new B();
mnaab.h();
}
} /** Output
MNA.A.B.h()
MNA.e()
MNA.f()
MNA.A.f()
MNA.A.g()
*/
//: Callbacks.java
interface Incrementable {
void increment();
}
class Callee1 implements Incrementable {
private int i = 0;
public void increment() {
i++;
System.out.println(i);
}
}
class MyIncrement {
public void increment() {
System.out.println("Other operation");
}
static void f(MyIncrement mi) {
mi.increment();
}
}
class Callee2 extends MyIncrement {
private int i = 0;
public void increment() {
super.increment();
i++;
System.out.println(i);
}
private class Closure implements Incrementable {
public void increment() {
Callee2.this.increment();
}
}
Incrementable getCallbackReference() {
return new Closure();
}
}
class Caller {
private Incrementable callbackReference;
Caller(Incrementable cbh) {
callbackReference = cbh;
}
void go() {
callbackReference.increment();
}
}
public class Callbacks {
public static void main(String[] args) {
Callee1 c1 = new Callee1();
Callee2 c2 = new Callee2();
MyIncrement.f(c2);
Caller caller1 = new Caller(c1);
Caller caller2 = new Caller(c2.getCallbackReference());
caller1.go();
caller1.go();
caller2.go();
caller2.go();
}
} /** Output
Other operation
1
1
2
Other operation
2
Other operation
3
*/
//: InheritInner.java
class WithInner {
class Inner {}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() { } // Error: 需要包含WithInner.Inner的封闭实例
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String args[]) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
}