JAVA编程思想3

嵌套接口:
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();
        }
    }

转载于:https://my.oschina.net/u/4058227/blog/3021009

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值