java 内部类

    xiaoxiao2025-07-24  21

    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 + " "); // foreach遍历 } } 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 // 外围类.this:可以用来在内部类返回对外围类对象的引用。 class DotThis{ // .this 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 // 外围类对象.New:可以用来创建内部类的对象。 class DotNew{ // .New 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(); // 在其他类创建DotNew内部类实例 } } --------------------- E:\java>java Test0526 false // 内部类与向上转型 interface Destination{ String readLabel(); } interface Contents{ int value(); } class Parcel{ private class PContents implements Contents{ // private内部类 private int i = 11; public int value(){ return i; } } protected class PDestination implements Destination{ // protected内部类 private String label; private PDestination( String whereTo ){ label = whereTo; } // private构造器 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() ); // 其他类无法访问private // Parcel.PContents pc = p.new PContents(); // 错误:无法访问private } } ---------------------- 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 ){ // private 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() ); } // TrackingSlip ts = new TrackingSlip( "slip" ); // 作用域外无法访问 } public void track(){ internalTracking( true ); } // 调用private 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
    最新回复(0)