标签:java
我们都知道java的类可以由public、default(缺省、不写)来修饰,分别表示的含义是允许公开访问以及只允许包内其它类访问,而同一包内的类通常是为完成同一个功能而协作。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; @Override public boolean end() { return i == items.length; } @Override public Object current() { return items[i]; } @Override 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.println(selector.current()); selector.next(); } } }
public 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(); } }
public class DotNew { public class Inner{}; public static void main(String[] args) { DotNew dn = new DotNew(); DotNew.Inner dni = dn.new Inner(); } }
abstract class Base{ public Base(int i) { System.out.println("Base constructor, i = " + i); } public abstract void f(); } public class AnonymousConstructor { private static int w = 7; public static Base getBase(int i,final int j) { return new Base(i) { { System.out.println("Inside instance initial"); } private int pj = j; private int pw = w; public void f() { System.out.println("In anonymous f()"); } }; } public static void main(String[] args) { Base base = AnonymousConstructor.getBase(47, 39); base.f(); } }
标签:java
原文地址:http://blog.csdn.net/troy__/article/details/41315679