java 内部类
内部类是什么?实例如下:
内部类是什么?
内部类:将一个类的定义放在另一个类定义的内部。
实例如下:
class Parcel{
class Contents{
private int i
= 11;
public int value(){ return i
; }
}
class Destination{
private String label
;
Destination( String label
){ this.label
= label
; }
String
readLabel(){ return label
; }
}
public Destination
to( String s
){ return new Destination( s
); }
public Contents
contents(){ return new Contents(); }
public void ship( String dest
){
Contents c
;
Destination d
;
c
= new Contents();
c
= contents();
d
= new Destination( dest
);
System
.out
.println( d
.readLabel() );
d
= to( dest
);
System
.out
.println( d
.readLabel() );
}
public static void main( String
[] args
){
Parcel p
;
p
= new Parcel();
p
.ship( "Tasmania" );
p
= new Parcel();
Parcel
.Contents c
= p
.contents();
Parcel
.Destination d
= p
.to( "Borneo" );
}
}
public class Test0526{
public static void main( String
[] args
){
Parcel
.main( args
);
}
}
----------------------
E
:\java
>java Test0526
Tasmania
Tasmania
interface Selector{
boolean end();
Object
current();
void next();
}
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();
}
System
.out
.println();
for( Object o
: sequence
.items
) System
.out
.print( o
+ " ");
}
}
public class Test0526{
public static void main( String
[] args
){
Sequence
.main( args
);
}
}
------------------------
E
:\java
>java Test0526
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
class DotThis{
void f(){ System
.out
.println( "DotThis.f()" ); }
public class Inner{
public DotThis
outer(){ return DotThis
.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();
System
.out
.println( dti
.outer() == dti
.outer() );
System
.out
.println( dti
.outer() == dt
);
}
}
public class Test0526{
public static void main( String
[] args
){
DotThis
.main( args
);
}
}
----------------------
E
:\java
>java Test0526
DoThis
.f()
true
true
class DotNew{
public class Inner{}
public static void main( String
[] args
){
DotNew dn
= new DotNew();
DotNew
.Inner dni
= dn
.new Inner();
System
.out
.println( dni
== (dn
.new Inner()) );
}
}
public class Test0526{
public static void main( String
[] args
){
DotNew
.main( args
);
DotNew
.Inner dni
= (new DotNew()).new Inner();
}
}
---------------------
E
:\java
>java Test0526
false
interface Destination{ String
readLabel(); }
interface Contents{ int value(); }
class Parcel{
private class PContents implements Contents{
private int i
= 11;
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 Test0526{
public static void main( String
[] args
){
Parcel p
= new Parcel();
Contents c
= p
.contents();
Destination d
= p
.destination( "Tasmania" );
System
.out
.println( d
.readLabel() );
}
}
----------------------
E
:\java
>java Test0526
Tasmania
interface Destination{ String
readLabel(); }
interface Contents{ int value(); }
class Parcel{
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
){
Parcel p
= new Parcel();
Destination d
= p
.destination( "Tasmania" );
System
.out
.println( d
.readLabel() );
}
}
public class Test0526{
public static void main( String
[] args
){
Parcel
.main( args
);
}
}
----------------------
E
:\java
>java Test0526
Tasmania
class Parcel{
private void internalTracking( boolean b
){
if( b
){
class TrackingSlip{
private String id
;
TrackingSlip( String s
){ id
= s
; }
String
getSlip(){ return id
; }
}
TrackingSlip ts
= new TrackingSlip( "slip" );
System
.out
.println( ts
.getSlip() );
}
}
public void track(){ internalTracking( true ); }
public static void main( String
[] args
){
Parcel p
= new Parcel();
p
.track();
}
}
public class Test0526{
public static void main( String
[] args
){
Parcel
.main( args
);
}
}
---------------------
E
:\java
>java Test0526
slip
interface Contents{ int value(); }
class Parcel{
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
){
Parcel p
= new Parcel();
Contents c
= p
.contents();
}
}
interface Contents{ int value(); }
class Parcel{
public Contents
contents(){
return new Contents(){
private int i
= 11;
public int value(){ return i
; }
};
}
public static void main( String
[] args
){
Parcel p
= new Parcel();
Contents c
= p
.contents();
}
}
class Wrapping{
private int i
;
public Wrapping( int x
){ i
= x
; }
public int value(){ return i
; }
}
class Parcel{
public Wrapping
wrapping( int x
){
return new Wrapping( x
){
public int value(){ return super.value() * 47; }
};
}
public static void main( String
[] args
){
Parcel p
= new Parcel();
Wrapping w
= p
.wrapping( 10 );
}
}
interface Destination{ String
readLabel(); }
class Parcel{
public Destination
destination( final String dest
){
return new Destination(){
private String label
= dest
;
public String
readLabel(){ return label
; }
};
}
public static void main( String
[] args
){
Parcel p
= new Parcel();
Destination d
= p
.destination( "Tasmania" );
}
}
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(); }
}
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();
}
}
public class Test0611{
public static void main( String
[] args
){
Callbacks
.main( args
);
}
}
-------------------------
E
:\java
>java Test0611
Other Operation
1
1
2
Other Operation
2
Other Operation
3