.this的使用
public class DotThis {
void f() {
System.out.println("dotThis.f()");
}
public class Inner{
public DotThis outer() {
//这个this是指的Inner的this
return DotThis.this;
}
}
public Inner inner() {
return new Inner();
}
public static void main(String[] args) {
DotThis dt=new DotThis();
DotThis.Inner dti=dt.inner();
DotThis outer = dti.outer();
outer.f();
}
}
.new的使用
public class DotNew {
public class Inner{}
public static void main(String[] args) {
DotNew d=new DotNew();
DotNew.Inner i=d.new Inner();
}
}
嵌套接口:static的
嵌套内部类:static
内部类:非static
局部内部类:只有在本方法中才能访问
public class Parcel5 {
public Destination destintion(String s) {
class PDestination implements Destination{
private String label;
private PDestination(String whereTo) {
this.label=whereTo;
}
@Override
public String readLabel() {
return label;
}
}
return new PDestination(s);
}
public static void main(String[] args) {
Parcel5 p=new Parcel5();
Destination destintion = p.destintion("tas");
System.out.println(destintion.readLabel());
}
}
方法内部 if语句内
public class Parcel6 {
private void internalTracking(boolean b) {
if(b) {
class TrackingSlip{
private String id;
public TrackingSlip(String s) {
id=s;
}
String getSlip() {return id;}
}
TrackingSlip t=new TrackingSlip("slip");
String s=t.getSlip();
System.out.println(s);
}
}
public static void main(String[] args) {
Parcel6 p = new Parcel6();
p.internalTracking(true);
}
}
匿名内部类:就是创建了一个目标类的子类或者叫做实现类
public class Parcle7 {
public Contents contents() {
return new Contents(){
private int i=11;
@Override
public int value() {
return i;
}
};
}
public static void main(String[] args) {
Parcle7 p=new Parcle7();
Contents c = p.contents();
System.out.println(c.value());
}
}
7的具体版为7b:
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) {
Parcel7b p=new Parcel7b();
Contents contents = p.contents();
}
//int i不用final 因为匿名内部类 没有使用 如果使用就得加final
public static Base getBase(int i) {
return new Base() {
{System.out.println("inside instance initlalizar");}
@Override
public void f() {
System.out.println("new Base().f()");
}
};
}
public static void main(String[] args) {
Base base=getBase(14);
base.f();
}
匿名内部类完成工厂模式
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
private Implementation1() {
}
@Override
public void method1() {
System.out.println("method1");
}
@Override
public void method2() {
System.out.println("method2");
}
public static ServiceFactory factory=
new ServiceFactory() {
public Service getService() {
return new Implementation1();
}
};
}
public class Factories {
public static void serviceCumster(ServiceFactory fact) {
Service service = fact.getService();
service.method1();
service.method2();
}
public static void main(String[] args) {
serviceCumster(Implementation1.factory);
}
}
嵌套类:静态的内部类
private String name;
private static class ParcelContents implements Contents{
private int i=11;
public int value() {
return i;}
}
protected static class ParcelDestination implements Destination{
private String label;
private ParcelDestination(String whereTo) {
label=whereTo;
}
public String readLabel() {return label;}
public static void f() {}
static int x=10;
static class AnotherLevel{
public static void f() {}
static int x=10;
}
}
public static Destination destination(String s) {
return new ParcelDestination(s);
}
public static Contents contents() {
return new ParcelContents();
}
public static void main(String[] args) {
Destination destination = destination("ad");
Contents contents = contents();
}
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();
}
}
}
public interface ClassInInterface {
void howdy();
class Test {
private String s="";
}
}
//多实现的方式1:单实现X,内部类实现Y
interface A{}
interface B{}
class X implements A,B{}
class Y implements A{
B makeB() {
return new B() {};
}
}
public class MultiInterfaces {
static void takesA(A a) {};
static void takesB(B b) {};
public static void main(String[] args) {
X x=new X();
Y y=new Y();
takesA(x);
takesA(y);
takesB(x);
takesB(y.makeB());
}
}
如果想继承抽象类那只能用匿名类了:
class D{}
abstract class E{}
class Z extends D{
E makeE() {
return new E() {
};
}
}
public class MultiImplemention {
static void takesD(D d) {};
static void takesE(E e) {};
public static void main(String[] args) {
Z z=new Z();
takesD(z);
takesE(z.makeE());
}
}
闭包与回调 代码懂 概念不懂
第一点是Caller1的目的是实现了接口 其他接口都是private的 caller2同样如此 是不是就叫做闭包?
回调是不是说的Incrementable getCallbackReference() {return new Closure();}返回了一个实现 然后通过这个实现在调用increment方法就叫做回调?
interface Incrementable{
void increment();
}
class Calleel implements Incrementable{
private int i=0;
@Override
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;
@Override
public void increment() {
super.increment();
i++;
System.out.println(i);
}
private class Closure implements Incrementable{
@Override
public void increment() {
Callee2.this.increment();
}
}
Incrementable getCallbackReference() {return new Closure();}
}
class Caller{
private Incrementable callbackReference;
public Caller(Incrementable cbh) {
this.callbackReference=cbh;
}
void go() {callbackReference.increment();}
}
public class Callbacks {
public static void main(String[] args) {
Calleel c1=new Calleel();
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 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 GreenhousrConrols extends Controller {
private boolean light=false;
public class LightOn extends Event{
public LightOn(long delayTime) {
super(delayTime);
}
@Override
public void action() {
light=true;
}
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);
// TODO Auto-generated constructor stub
}
@Override
public void action() {
water=true;
}
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);
// TODO Auto-generated constructor stub
}
@Override
public void action() {
// TODO Auto-generated method stub
thermostat="Night";
}
@Override
public String toString() {
return "ThermostatNight on night setting";
}
}
public class ThermostatDay extends Event{
public ThermostatDay(long delayTime) {
super(delayTime);
}
@Override
public void action() {
thermostat="Night";
}
@Override
public String toString() {
return "ThermostatDay 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 "Belling";
}
}
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 "Terminate";
}
}
}
public class GreenhouseController {
public static void main(String[] args) {
GreenhousrConrols gc=new GreenhousrConrols();
gc.addEvent(gc.new Bell(9000));
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 GreenhousrConrols.Terminate(new Integer(args[0])));
}
gc.run();
}
}
继承内部类:构造函数需要这么写
class WithInner{
class Inner{}
}
public class InheritInner extends WithInner.Inner{
public InheritInner(WithInner i) {
i.super();
}
public static void main(String[] args) {
WithInner wi=new WithInner();
InheritInner ii = new InheritInner(wi);
}
}
内部类继承需要指向具体的位置否则继承外围类再写同名的内部类是完全不同的两个东西
//局部内部类定义在方法里的