嵌套接口:
1:嵌套类可以实现private的interface----Dimpl和DImpl2实现private的D接口但是在嵌套类内部
2:
class A {
interface B{void f();}
public class BImpl implements B{public void f() {}}
private class BImpl2 implements B{public void f() {}}
public interface C{void f();}
class CImpl implements C{public void f() {}}
private class CImpl2 implements C{public void f() {}}
private interface D{void f();}
private class DImpl implements D{public void f() {}}
public class DImpl2 implements D{public void f() {}}
public D getD() {return new DImpl2();}
private D dRef;
public void receiveD(D d) {
dRef=d;
dRef.f();
}
}
interface E{
interface G{void f();}
public interface H{void f();}
void g();
}
public class NestingInterfaces{
public class BImp implements A.B{public void f() {}}
class CImpl implements A.C{public void f() {}}
//class Dimpl implements A.D
class EImp implements E{public void g() {}}
class EGimp implements E.G{public void f() {}}
class EImp2 implements E{
public void g() {}
class EG implements E.G{public void f() {}}
}
public static void main(String[] args) {
A a=new A();
}
}
工厂模式代码:
interface Service{
void method1();
void method2();
}
interface ServiceFactory{
Service getService();
}
class Implementation1 implements Service{
public Implementation1() {}
@Override
public void method1() {
System.out.println("implements1 method1");
}
@Override
public void method2() {
System.out.println("implements1 method2");
}
}
class Implementation1Factory implements ServiceFactory{
@Override
public Service getService() {
return new Implementation1();
}
}
class Implementation2 implements Service{
public Implementation2() {}
@Override
public void method1() {
System.out.println("implement2s method1");
}
@Override
public void method2() {
System.out.println("implement2s method2");
}
}
class Implementation2Factory implements ServiceFactory{
@Override
public Service getService() {
return new Implementation2();
}
}
public class Factorys {
public static void serviceConsumer(ServiceFactory fact) {
Service service=fact.getService();
service.method1();
service.method2();
}
public static void main(String[] args) {
serviceConsumer(new Implementation1Factory());
serviceConsumer(new Implementation2Factory());
}
}
适配接口没看懂 但是能写出来一个适配~~~回头再看一眼。。331页
好经典的例子: 解释了内部类的调用 和序列的执行
interface Seletor{
boolean end();
Object crruent();
void next();
}
public class Sequence {
private Object[] items;
private int next=0;
public Sequence(int x) {
items=new Object[x];
}
public void add(Object x) {
if(next<items.length) {
items[next++]=x;
}
}
private class SequenceSelector implements Seletor{
private int i=0;
public boolean end() {return i==items.length;}
public Object crruent() {
return items[i];
}
public void next() {
if(i<items.length) {
i++;
}
}
}
public Seletor seletor() {
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));
}
Seletor seletor=sequence.seletor();
while(!seletor.end()) {
System.out.println(seletor.crruent()+" ");
seletor.next();
}
}