Java的内部静态类
2011-05-27 13:51 会被淹死的鱼 阅读(939) 评论(0) 编辑 收藏 举报Java的内部类语法比较复杂,只有内部静态类,而没有静态类。
public class Hello { public class Child { public Child() {} public void sayHello() { System.out.println("Hello from child!"); } } public static void main(String[] args) { System.out.println("Hello, World!"); Hello h = new Hello(); Child child = h.new Child(); child.sayHello(); } }
上面的例子用到了.new的语法,因为main是个静态方法,所以Hello类不会被初始化,Child类也不会被加载到内存中,必须显示地初始化Hello类,然后使用.new的语法来初始化内部类。当然还可以使用内部静态类。
public final class Hello { public static class Child { public Child() { } public void sayHello() { System.out.println("Hello from child!"); } } public static void main(String[] args) { System.out.println("Hello, World!"); Child child = new Hello.Child(); child.sayHello(); } }
这里因为内部的Child类是static,所以即使Hello没有初始化,Child也已经加载到内存中了。
这样的处理方式和类中的static成员是一致的,内部类可以随意的访问外部类,这是内部类最大的特点。
public class Outter { public class Inner { private int in = 3; public void print() { System.out.println(Outter.this.out); } } private int out = 4; public void print() { System.out.println(new Inner().in); } public static void main(String[] args) { new Outter().new Inner().print(); } }
诡异的内部类语法
内部类的使用,下面是一个command模式的例子,摘自Java编程思想(Thinking in java)
Event.java
public abstract class Event { private long eventTime; protected final long delayTime; public Event(long delayTime) { super(); this.delayTime = delayTime; start(); } public void start() { eventTime = System.nanoTime() + delayTime; } public boolean ready() { return System.nanoTime() >= eventTime; } public abstract void action(); }
Controller.java
import java.util.ArrayList; import java.util.List; public class Controller { private List<Event> eventList = new ArrayList<Event>(); public void addEvent(Event c) { eventList.add(c); } public void run() { while (eventList.size() > 0) { for (Event e : new ArrayList<Event>(eventList)) { if (e.ready()) { System.out.println(e); e.action(); eventList.remove(e); } } } } }
GreenhouseControls.java
public class GreenhouseControls extends Controller { private boolean light = false; public class LightOn extends Event { public LightOn(long delayTime) { super(delayTime); } @Override public void action() { light = true; } @Override public String toString() { return "Light is on."; } } public class LightOff extends Event { public LightOff(long delayTime) { super(delayTime); } @Override public void action() { light = false; } @Override public String toString() { return "Light is off."; } } private boolean water = false; public class WaterOn extends Event { public WaterOn(long delayTime) { super(delayTime); } @Override public void action() { water = true; } @Override public String toString() { return "Water is on."; } } public class WaterOff extends Event { public WaterOff(long delayTime) { super(delayTime); } @Override public void action() { water = false; } @Override public String toString() { return "Water is off."; } } // 恒温器 空调 private String thermostat = "Day"; public class ThermostatNight extends Event { public ThermostatNight(long delayTime) { super(delayTime); } @Override public void action() { thermostat = "Night"; } @Override public String toString() { return "Thermostat on night setting."; } } public class ThermostatDay extends Event { public ThermostatDay(long delayTime) { super(delayTime); } @Override public void action() { thermostat = "Day"; } @Override public String toString() { return "Thermostat on day setting."; } } public class Bell extends Event { public Bell(long delayTime) { super(delayTime); } @Override public void action() { addEvent(new Bell(delayTime)); } @Override public String toString() { return "Bing!"; } } public class Restart extends Event { private Event[] eventList; public Restart(long delayTime, Event[] eventList) { super(delayTime); this.eventList = eventList; for (Event e : eventList) addEvent(e); } @Override public void action() { for (Event e: eventList) { e.start(); addEvent(e); } start(); addEvent(this); } @Override public String toString() { return "Restarting system."; } } public static class Terminate extends Event { public Terminate(long delayTime) { super(delayTime); } @Override public void action() { System.exit(0); } @Override public String toString() { return "Terminating."; } } }
GreenhouseController.java
public class GreenhouseController { public static void main(String[] args) { GreenhouseControls gc = new GreenhouseControls(); gc.addEvent(gc.new Bell(900)); Event[] eventList = { gc.new ThermostatNight(0), gc.new LightOn(200), gc.new LightOff(400), gc.new WaterOn(600), gc.new WaterOff(800), gc.new ThermostatDay(1400) }; gc.addEvent(gc.new Restart(2000, eventList)); if (args.length == 1) { gc.addEvent(new GreenhouseControls.Terminate(new Integer(args[0]))); } gc.run(); } }
作者:icejoywoo
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
短网址: http://goo.gl/ZiZCi