码迷,mamicode.com
首页 > 编程语言 > 详细

Java编程思想:内部类基础部分

时间:2019-03-16 20:47:29      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:col   理解   select   制造   i++   实验   inter   this   外部类   

public class Test {
    public static void main(String[] args) {
//        Parcel1.test();
//        Parcel2.test();
//        Sequence.test();
        DotThis.test();
    }
}

class Parcel1 {

    class Contents {
        private int i = 11;

        public int value() {
            return i;
        }
    }

    class Destination {
        private String label;

        Destination(String whereTo) {
            label = whereTo;
        }

        String readLabel() {
            return label;
        }
    }

    public void ship(String dest) {
        Contents c = new Contents();
        Destination d = new Destination(dest);

        System.out.println(d.readLabel());
    }

    public static void test() {
        Parcel1 p = new Parcel1();
        p.ship("A");
    }


}

/*
    更典型的情况是:外部类将有一个方法,该方法返回一个指向内部类的引用。

    截止到目前,内部类都是有方案被更换的
 */
class Parcel2 {
    class Contents {
        private int i = 11;

        public int value() {
            return i;
        }
    }

    class Destination {
        private String label;

        Destination(String whereTo) {
            label = whereTo;
        }

        String readLabel() {
            return label;
        }
    }

    public Destination to(String s) {
        return new Destination(s);
    }

    public Contents contents() {
        return new Contents();
    }

    public void ship(String dest) {
        //上一个实验中,这儿是手动new出来的
        Contents c = contents();
        Destination d = to(dest);
        System.out.println(d.readLabel());
    }

    public static void test() {
        Parcel2 p = new Parcel2();
        p.ship("B");

        Parcel2 p2 = new Parcel2();
        Parcel2.Destination d = p2.to("C");
        Parcel2.Contents c = p2.contents();
    }


}

/*
    当生成一个内部类的对象时,此对象与制造它的外围对象之间就有了一种联系,所以它能访问
    其他外围对象的所有成员,而不需要任何特殊条件。此外,内部类还拥有其外部类的所有元素
    的访问权。

    我突然想到了一件事,实现Iterable接口的iterator()方法时,我们就返回的是一个内部
    类。一直不理解这个内部类设计的好处,现在发现自己一直在用它。
 */

/*
    当某个外部类的对象创建了一个内部类对象时,此内部类对象必定会秘密的捕获一个指向那个
    外部类的引用。内部类对象只能在与其外围类的对象相关联的情况下才能被创建(在内部类是
    非static类时)。
 */

interface Selector {
    boolean end();

    Object current();

    void next();
}

class Sequence {
    private Object[] items;
    private int next = 0;

    public Sequence(int size) {
        items = new Object[size];
    }

    public void add(Object x) {
        if (next < items.length) {
            items[next]=x;
            next++;
        }
    }

    private class SequenceSelector implements Selector {
        private int i = 0;
        @Override
        public boolean end() {
            /*
                书中的逻辑:return i == items.length;
                我觉得不太严谨,又没有说,一定要把items装满,除非你想取到空元素
                我的逻辑:
                    return i >= next;

             */
            return i >= next;
        }

        @Override
        public Object current() {
            if (i < next) {
                return items[i];
            }
            return null;
        }

        @Override
        public void next() {
            i++;
        }
    }

    private Selector selector() {
        return new SequenceSelector();
    }

    public static void test() {
        Sequence sequence = new Sequence(100);
        for (int i = 0; i < 10; i++) {
            sequence.add(Integer.toString(i));
        }

        Selector selector = sequence.selector();
        while (!selector.end()) {
            System.out.println(selector.current()+" ");
            selector.next();
        }
    }

}

/*
    需求:
        如果你在内部类中需要生成对外部类的对象的引用,可以使用外部类的名字后面紧跟圆点和
        this。
 */
class DotThis {
    void f() {
        System.out.println("DotThis.f()");
    }

    public class Inner {
        public DotThis outer() {
            return DotThis.this;
        }
    }

    public Inner inner() {
        return new Inner();
    }

    public static void test() {
        DotThis outter = new DotThis();
        DotThis.Inner inner = outter.inner();

        inner.outer().f();
    }

}

/*
    需求:
        有时,你可能想要告知某些其他对象,去创建某个内部类的对象。要实现这个目的,你必须
        在new表达式中提供对其他外部类对象的引用,这就需要.new语法。

            ——怎么告知?

        我感觉这就是一个,创建内部类对象的语法

        想要直接创建内部类的对象,不能去引用外部内的名字,而必须使用外部类的对象来创建该
        内部类的对象,就像在上面的程序中所看到的那样。
 */

class DotNew {
    public class Inner {

    }

    public static void test() {
        DotNew dn = new DotNew();
        /*
            这种编码果然创建不了内部类的对象,编译器报错说:DotNew.this cannot 关联到
            一个静态的上下文。联系上文说的,创建一个内部类时是需要捕捉一个外围内的引用的
            这种方式得不到外围类的引用, 所以无法完成创建。

            DotNew.Inner di = new DotNew.Inner();
         */
        DotNew.Inner di = dn.new Inner();
    }
}

class Parcel3 {
    class Contents {
        private int i = 11;

        public int value() {
            return i;
        }
    }

    class Destination {
        private String label;

        Destination(String whereTo) {
            label = whereTo;
        }

        String readLabel() {
            return label;
        }
    }

    public static void test() {
        Parcel3 p = new Parcel3();

        Parcel3.Contents c = p.new Contents();
        Parcel3.Destination d = p.new Destination("D");
    }

}

 

Java编程思想:内部类基础部分

标签:col   理解   select   制造   i++   实验   inter   this   外部类   

原文地址:https://www.cnblogs.com/junjie2019/p/10544225.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!