内部类

来源:互联网 发布:淘宝怎么找学位证办理 编辑:程序博客网 时间:2024/05/16 10:12

1.匿名内部类

1.如果基类有一个有参数的构造器,可以在创建匿名类时来传递。
代码例子:

abstract class Base {  public Base(int i) {    print("Base constructor, i = " + i);  }  public abstract void f();}   public class AnonymousConstructor {  public static Base getBase(int i) {    return new Base(i) {      { print("Inside instance initializer"); }      public void f() {        print("In anonymous f()");      }    };  }  public static void main(String[] args) {    Base base = getBase(47);    base.f();  }} 

运行结果:
Base constructor, i = 47
Inside instance initializer
In anonymous f()

2.嵌套类

1.接口内部可以放置嵌套类作为接口的一部分,甚至可以实现这个接口。
代码例子:

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();    }  }}

运行结果:
Howdy!
2.一个内部类被嵌套多少层并不重要-它能透明地访问所有它所嵌入的外围类的所有成员.
代码例子:

class MNA {  private void f() {}  class A {    private void g() {}    public class B {      void h() {        g();        f();      }    }  }}   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();  }}

3.为什么要使用内部类

1.内部类的一些特殊特性.

  • 内部类可以有多个实例,每个实例都有自己的状态信息,并且与其外围类对象的信息相互独立。
  • 在单个外围类中,可以让多个内部类以不同的方式实现同一个接口,或继承同一个类。
  • 创建内部类对象的时刻并不依赖于外围类对象的创建。

2.控制框架是一类特殊的应用程序框架,它用来解决响应事件的需求,(主要用来响应时间的系统被称作事件驱动系统),它的工作就是在事件”就绪”的时候执行事件(就绪可以基于任何事件的触发),接下来的问题就是,对于要控制什么,控制框架并不包含任何具体的信息,那些信息是在实现算法的action()部分时,通过继承来提供,如图:
控制框架
代码例子:

public abstract class Event {  private long eventTime;  protected final long delayTime;  public Event(long delayTime) {    this.delayTime = delayTime;    start();  }  //定制事件启动时间  public void start() {     eventTime = System.nanoTime() + delayTime;  }  //判断当前时间是否达到了事件就绪的执行时间  public boolean ready() {    return System.nanoTime() >= eventTime;  }  //响应事件  public abstract void action();}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);        }  }} public class GreenhouseControls extends Controller {  private boolean light = false;  public class LightOn extends Event {    public LightOn(long delayTime) { super(delayTime); }    public void action() {           light = true;    }    public String toString() { return "Light is on"; }  }   public class LightOff extends Event {    public LightOff(long delayTime) { super(delayTime); }    public void action() {            light = false;    }    public String toString() { return "Light is off"; }  }  private boolean water = false;  public class WaterOn extends Event {    public WaterOn(long delayTime) { super(delayTime); }    public void action() {           water = true;    }    public String toString() {      return "Greenhouse water is on";    }  }   public class WaterOff extends Event {    public WaterOff(long delayTime) { super(delayTime); }    public void action() {           water = false;    }    public String toString() {      return "Greenhouse water is off";    }  }  private String thermostat = "Day";      public class ThermostatNight extends Event {    public ThermostatNight(long delayTime) {      super(delayTime);    }    public void action() {           thermostat = "Night";    }    public String toString() {      return "Thermostat on night setting";    }  }   public class ThermostatDay extends Event {    public ThermostatDay(long delayTime) {      super(delayTime);    }    public void action() {          thermostat = "Day";    }    public String toString() {      return "Thermostat on day setting";    }  }    public class Bell extends Event {    public Bell(long delayTime) { super(delayTime); }    //设定新的Bell事件    public void action() {      addEvent(new Bell(delayTime));    }    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);    }    public void action() {      for(Event e : eventList) {      //重新设定列表的事件        e.start();         addEvent(e);      }      //重新设定这个事件      start();       addEvent(this);    }    public String toString() {      return "Restarting system";    }  }  //终止所有事件    public static class Terminate extends Event {    public Terminate(long delayTime) { super(delayTime); }    public void action() { System.exit(0); }    public String toString() { return "Terminating";  }  }} 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();  }}

运行结果:
Bing!
Thermostat on night setting
Light is on
Light is off
Greenhouse water is on
Greenhouse water is off
Thermostat on day setting
Restarting system
Terminating
这个过程的流程图:
流程图
3.因为内部类的构造器必须连接到指向其外围类对象的引用,所以在继承内部类的时候,那个指向外围类对象的”秘密的”引用必须被初始化,而在导出类中不再存在可连接的默认对象。
代码例子:

class WithInner {  class Inner {}}public class InheritInner extends WithInner.Inner {  //编译不能通过  //! InheritInner() {}   InheritInner(WithInner wi) {    wi.super();  }  public static void main(String[] args) {    WithInner wi = new WithInner();    InheritInner ii = new InheritInner(wi);  }}

这篇博客参考资料:
thinking in java

0 0
原创粉丝点击